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端口,也可以自己放入配置文件讀取

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM