/// <summary> /// 注册分布式锁,实例对象IDistributedLock /// </summary> /// <param name="hostBuilder"></param> /// <param name="action"></param> /// <returns></returns> public static IFlashHostBuilder AddRedisDistributedLock(this IFlashHostBuilder hostBuilder, Action <RedisCacheConfig> action) { action = action ?? throw new ArgumentNullException(nameof(action)); var config = new RedisCacheConfig(); action(config); hostBuilder.Services.TryAddSingleton <IDistributedLock>(new DistributedLock(CacheFactory.Build(config))); return(hostBuilder); }
public static ICacheManager Build(Action <RedisCacheConfig> action) { var option = new RedisCacheConfig(); action(option); var cacheManager = RedisCacheManage.Create(option); return(cacheManager); }
public void InstallServices(IServiceCollection services, IConfiguration Configuration) { var redisCacheConfig = new RedisCacheConfig(); Configuration.GetSection(nameof(RedisCacheConfig)).Bind(redisCacheConfig); services.AddSingleton(redisCacheConfig); //if (redisCacheConfig.IsEnabled == false) // return; services.AddStackExchangeRedisCache(options => { options.Configuration = redisCacheConfig.ConnectionString; }); services.AddSingleton <IRedisCacheService, RedisCacheService>(); }
/// <summary> /// 注册Redis缓存管理,实例对象ICacheManager /// </summary> /// <param name="cacheBuilder"></param> /// <param name="action"></param> /// <returns></returns> public static IFlashCacheBuilder AddRedis(this IFlashCacheBuilder cacheBuilder, Action <ICacheConfig> action) { action = action ?? throw new ArgumentNullException(nameof(action)); var option = new RedisCacheConfig(); action(option); var cacheManager = CacheFactory.Build(option); cacheBuilder.Services.AddSingleton <ICacheManager>(cacheManager); if (option.DistributedLock) { cacheBuilder.Services.TryAddSingleton <IDistributedLock>(new DistributedLock(cacheManager)); } return(cacheBuilder); }
public RedisCache(IConfig configProvider) { _config = configProvider.GetRedisCacheConfig(); }
public RedisCacheService(IDistributedCache distributedCache, RedisCacheConfig redisCacheConfig) { _distributedCache = distributedCache; _redisCacheConfig = redisCacheConfig; }
/// <summary> /// 创建链接池管理对象 /// </summary> public static RedisCacheManage Create(RedisCacheConfig config) { _KeyPrefix = config.KeyPrefix + ":"; if (_Locator == null) { lock (_syncCreateInstance) { if (_Locator == null) { if (string.IsNullOrEmpty(config.SentineList) || !_supportSentinal) { //Redis服务器相关配置 string writeServerList = config.WriteServerList; string readServerList = config.ReadServerList; var writeServerArray = RedisCacheConfigHelper.SplitString(writeServerList, ",").ToList(); var readServerArray = RedisCacheConfigHelper.SplitString(readServerList, ",").ToList(); var Nodes = new List <string>(); //只有一个写,多个读的情况 /* * Redis.ReadServerList 192.168.100.51:16378,192.168.100.51:26378 * Redis.WriteServerList 192.168.100.51:6378 */ if (writeServerArray.Count == 1) { var writeServer = writeServerArray[0]; var NodeName = writeServerArray[0]; if (!_clusterConfigOptions.ContainsKey(NodeName)) { StackExchange.Redis.ConfigurationOptions configOption = new StackExchange.Redis.ConfigurationOptions(); configOption.ServiceName = NodeName; configOption.Password = config.Password; configOption.AbortOnConnectFail = false; configOption.DefaultDatabase = config.DBNum; configOption.Ssl = config.Ssl; foreach (var ipAndPort in writeServerArray.Union(readServerArray)) { configOption.EndPoints.Add(ipAndPort); } _clusterConfigOptions.Add(writeServer, configOption); } Nodes.Add(NodeName); } /* * 多个写和多个读 * Redis.ReadServerList [email protected]:16378,[email protected]:16379,[email protected]:16380,[email protected]:16381,[email protected]:16382,[email protected]:26378,[email protected]:26379,[email protected]:26380,[email protected]:26381,[email protected]:26382 * Redis.WriteServerList [email protected]:6378,[email protected]:6379,[email protected]:6380,[email protected]:6381,[email protected]:6382 */ else { for (int i = 0; i < writeServerArray.Count; i++) { //存在多个Master服务器的时候 if (writeServerArray[i].IndexOf("@") > 0) { //集群名称() var NodeName = RedisCacheConfigHelper.GetServerClusterName(writeServerArray[i]); //主服务器名称 var masterServer = RedisCacheConfigHelper.GetServerHost(writeServerArray[i]); //主服务器列表 var masterServerIPAndPortArray = RedisCacheConfigHelper.GetServerList(config.WriteServerList, NodeName); //从服务器列表 var slaveServerIPAndPortArray = RedisCacheConfigHelper.GetServerList(config.ReadServerList, NodeName); //当前集群的配置不存在 if (!_clusterConfigOptions.ContainsKey(NodeName)) { StackExchange.Redis.ConfigurationOptions configOption = new StackExchange.Redis.ConfigurationOptions(); configOption.ServiceName = NodeName; configOption.Password = config.Password; configOption.AbortOnConnectFail = false; configOption.DefaultDatabase = config.DBNum; configOption.Ssl = config.Ssl; foreach (var ipAndPort in masterServerIPAndPortArray.Union(slaveServerIPAndPortArray).Distinct()) { configOption.EndPoints.Add(RedisCacheConfigHelper.GetIP(ipAndPort), RedisCacheConfigHelper.GetPort(ipAndPort)); } _clusterConfigOptions.Add(NodeName, configOption); } Nodes.Add(NodeName); } else { //192.168.10.100:6379 var NodeName = writeServerArray[i]; if (!_clusterConfigOptions.ContainsKey(NodeName)) { StackExchange.Redis.ConfigurationOptions configOption = new StackExchange.Redis.ConfigurationOptions(); configOption.ServiceName = NodeName; configOption.Password = config.Password; configOption.AbortOnConnectFail = false; configOption.DefaultDatabase = config.DBNum; configOption.Ssl = config.Ssl; configOption.EndPoints.Add(RedisCacheConfigHelper.GetIP(NodeName), RedisCacheConfigHelper.GetPort(NodeName)); _clusterConfigOptions.Add(NodeName, configOption); } Nodes.Add(NodeName); } } } _Locator = new KetamaHash.KetamaNodeLocator(Nodes, _VIRTUAL_NODE_COUNT); } else { List <string> sentinelMasterNameList = new List <string>(); List <string> sentinelServerHostList = new List <string>(); var SentineList = RedisCacheConfigHelper.SplitString(config.SentineList, ",").ToList(); for (int i = 0; i < SentineList.Count; i++) { var args = RedisCacheConfigHelper.SplitString(SentineList[i], "@").ToList(); var ServiceName = args[0]; var hostName = args[1]; var endPoint = RedisCacheConfigHelper.SplitString(hostName, ":").ToList(); var ip = endPoint[0]; //IP var port = int.Parse(endPoint[1]); //端口 sentinelMasterNameList.Add(ServiceName); sentinelServerHostList.Add(hostName); if (!_clusterConfigOptions.ContainsKey(hostName)) { //连接sentinel服务器 StackExchange.Redis.ConfigurationOptions sentinelConfig = new StackExchange.Redis.ConfigurationOptions(); sentinelConfig.ServiceName = ServiceName; sentinelConfig.EndPoints.Add(ip, port); sentinelConfig.AbortOnConnectFail = false; sentinelConfig.DefaultDatabase = config.DBNum; sentinelConfig.TieBreaker = ""; //这行在sentinel模式必须加上 sentinelConfig.CommandMap = StackExchange.Redis.CommandMap.Sentinel; sentinelConfig.DefaultVersion = new Version(3, 0); _clusterConfigOptions[hostName] = sentinelConfig; } else { StackExchange.Redis.ConfigurationOptions sentinelConfig = _clusterConfigOptions[hostName] as StackExchange.Redis.ConfigurationOptions; sentinelConfig.EndPoints.Add(ip, port); _clusterConfigOptions[hostName] = sentinelConfig; } } //初始化Reds分片定位器 _Locator = new KetamaHash.KetamaNodeLocator(sentinelServerHostList, _VIRTUAL_NODE_COUNT); } } } } return(new RedisCacheManage(config.DBNum)); }
public Startup(IConfiguration configuration) { Configuration = configuration; cacheProvider = new RedisCacheConfig(); }
public static ICacheManager Build(RedisCacheConfig option) { var cacheManager = RedisCacheManage.Create(option); return(cacheManager); }