netcore集成redis
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = connectionString;
options.InstanceName = instanceName;
});
1、集成redis集群
//单主机模式
//"Host": "XXX:6379,password=XXX,connectTimeout=3000,connectRetry=3,DefaultDatabase=0 ",
//集群模式
"Host": "XXX:6379,XXX:6379,192.168.3.46:6379,serviceName=mymaster,password=XXX,connectTimeout=3000,connectRetry=3,KeepAlive=180,DefaultDatabase=0,$CLIENT=,$CLUSTER=,$CONFIG=,$ECHO=,$INFO=,$PING=",
2、使用哨兵模式集成redis集群
//哨兵模式
"Host": "XXX:26379,XXX:26379,XXX:26379,serviceName=mymaster,password=XXX,connectTimeout=3000,connectRetry=3,KeepAlive=180,DefaultDatabase=0,allowAdmin = true"
完整的配置: "Redis": {
//单主机模式
//"Host": "XXX:6379,password=XXX,connectTimeout=3000,connectRetry=3,DefaultDatabase=0 ",
//集群模式
//"Host": "XXX:6379,192.168.3.45:6379,XXX:6379,serviceName=mymaster,password=XXXX,connectTimeout=3000,connectRetry=3,KeepAlive=180,DefaultDatabase=0,$CLIENT=,$CLUSTER=,$CONFIG=,$ECHO=,$INFO=,$PING=",
//哨兵模式
"Host": "XXXX:26379,XXXX:26379,XXX:26379,serviceName=mymaster,password=XXXX,connectTimeout=3000,connectRetry=3,KeepAlive=180,DefaultDatabase=0,allowAdmin = true"
},
如何使用?
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Service.Common.Autofac.Attributes;
using Service.Common.helpers;
using Service.Common.utils;
using Utils.Common.utils;
namespace Service.Common.Services
{
///
///
///
[Se