Abp Vnext 替换Redis的实现为csredis

Host项目安装csredis的nuget包和data protect包并

移除 Microsoft.Extensions.Caching.StackExchangeRedis Microsoft.AspNetCore.DataProtection.StackExchangeRedis 

添加

Cacheing.CSRedis

AspNetCore.DataProtection.CSRedis

 

 

 

 

 

 

然后在xxModule.cs修改

添加using

using CSRedis;

using Microsoft.Extensions.Caching.Distributed;

 

在ConfigureService()中找到这段儿代码

context.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = configuration["Redis:Configuration"];
};

if (!hostingEnvironment.IsDevelopment())
{
  var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
  context.Services.AddDataProtection()
      .PersistKeysToStackExchangeRedis(redis, "XXX-DataProtection-Keys");
}

 

替换为

var connection = new CSRedisClient(configuration["Redis:Configuration"]);
            context.Services.AddSingleton<IDistributedCache>(new Microsoft.Extensions.Caching.Redis.CSRedisCache(connection));
RedisHelper.Initialization(connection);//如果需要使用csredis提供的redis原生功能则需要加此行来初始化csredis的单例
if (!hostingEnvironment.IsDevelopment()) { context.Services .AddDataProtection() .PersistKeysToCSRedis(connection, "XXX-Protection-Keys"); }

 

PS:连接串如果有密码或者更改databaseId的话需要按照csredis的规则来,也就是原始的redis连接串格

"Redis": {

    "Configuration": "127.0.0.1:6379,password=123,defaultDatabase=13,prefix=my_"

},

如果是哨兵集群则需要在把上文中new CSRedisClient(configuration["Redis:Configuration"])改为new CSRedisClient(configuration["Redis:Configuration"], new []{ "192.169.1.10:26379", "192.169.1.11:26379", "192.169.1.12:26379"}),后边那三个是哨兵节点ip端口,也可以自己放入配置文件读取

 

posted @ 2020-06-22 17:25  turingguo  阅读(1336)  评论(0编辑  收藏  举报