飞扬的Blog
主页
登录
asp.net core Redis
"date"
:
"2021-08-11 01:13:14"
"classfiy"
:
"asp.net core"
"author"
:
"飞扬"
"viewTimes"
:
266
返回
[TOC] # 第三方库 `CSRedisCore`:https://github.com/2881099/csredis 用法:https://www.cnblogs.com/yaopengfei/p/14211883.html # 简单用法 ## 安装 Redis >下载地址:https://github.com/tporadowski/redis/releases/ ### 配置Redis - 设置密码: `C:\Program Files\Redis\redis.windows-service.conf` 文件修改配置:`requirepass 123` - CMD: `net stop redis` `net start redis` ## 安装 Nuget 包 >- `Install-Package StackExchange.Redis` - `Install-Package Microsoft.Extensions.Caching.Redis` ## 配置 appsettings.json ```json "ConnectionStrings": { "Redis": "127.0.0.1:6379,password=123,defaultdatabase=0" }, ``` ## Startup.cs 配置 Redis ```csharp services.AddDistributedRedisCache(options => { options.Configuration = Configuration.GetConnectionString("Redis"); }); ``` ## 使用 Redis ```csharp distributed.SetString("test", "value", new DistributedCacheEntryOptions() { //设置一个过期时间,每次访问过后会刷新过期时间 SlidingExpiration = TimeSpan.FromSeconds(10), //设置一个相对于当前时间的过期时间,访问不会重置时间 AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1) }); var value = distributed.GetString("test"); ``` ## Redis 扩展类 ```csharp using Microsoft.Extensions.Caching.Distributed; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Microsoft.Extensions.Caching.Distributed { public static class DistributedCacheExtensions { public static void Set(this IDistributedCache cache, string key, string value) { cache.SetString(key, value); } public static void Set(this IDistributedCache cache, string key, string value, DistributedCacheEntryOptions options) { cache.SetString(key, value, options); } public static async Task SetAsync(this IDistributedCache cache, string key, string value) { await cache.SetStringAsync(key, value); } public static async Task SetAsync(this IDistributedCache cache, string key, string value, DistributedCacheEntryOptions options) { await cache.SetStringAsync(key, value, options); } public static void Set<T>(this IDistributedCache cache, string key, T value) { var text = JsonConvert.SerializeObject(value); cache.SetString(key, text); } public static void Set<T>(this IDistributedCache cache, string key, T value, DistributedCacheEntryOptions options) { var text = JsonConvert.SerializeObject(value); cache.SetString(key, text, options); } public static async Task SetAsync<T>(this IDistributedCache cache, string key, T value) { var text = JsonConvert.SerializeObject(value); await cache.SetStringAsync(key, text); } public static async Task SetAsync<T>(this IDistributedCache cache, string key, T value, DistributedCacheEntryOptions options) { var text = JsonConvert.SerializeObject(value); await cache.SetStringAsync(key, text, options); } public static T Get<T>(this IDistributedCache cache, string key) { var value = cache.GetString(key); if (string.IsNullOrEmpty(value)) return default; var model = JsonConvert.DeserializeObject<T>(value); return model; } public static async Task<T> GetAsync<T>(this IDistributedCache cache, string key) { var value = await cache.GetStringAsync(key); if (string.IsNullOrEmpty(value)) return default; var model = JsonConvert.DeserializeObject<T>(value); return model; } } } ``` # 进阶用法 >- https://www.chengxulvtu.com/stackexchange-redis-in-aspnet-core-3-1/ - StackExchange.Redis官方文档:https://imperugo.gitbook.io/stackexchange-redis-extensions/