public IDistributedCache Copy()
        {
            var copyRedis = new MAMRedisDistributedCache();

            copyRedis.Set(_databaseIndex, _config);
            return(copyRedis);
        }
Ejemplo n.º 2
0
        public static MAMRedisDistributedCache Build(List <RedisConectionModel> connections, Action <string, string> handle = null)
        {
            if (null == connections ||
                0 == connections.Count)
            {
                throw new Exception("没有找到redis连接信息");
            }

            var cache = new MAMRedisDistributedCache();

            var masterServerName = connections.FirstOrDefault(f => !string.IsNullOrEmpty(f.ServiceName))?.ServiceName;
            var password         = connections.FirstOrDefault(f => !string.IsNullOrEmpty(f.Password))?.Password;
            var db = int.Parse(connections.FirstOrDefault().Database);

            if (!string.IsNullOrEmpty(masterServerName))
            {
                var configOptions = new ConfigurationOptions
                {
                    ServiceName          = masterServerName,
                    TieBreaker           = "",
                    CommandMap           = CommandMap.Sentinel,
                    DefaultVersion       = new Version(3, 0),
                    AllowAdmin           = true,
                    SyncTimeout          = 1000,
                    ReconnectRetryPolicy = new ExponentialRetry(10000),
                    ConfigCheckSeconds   = 10,
                };

                foreach (var connectItem in connections)
                {
                    configOptions.EndPoints.Add(connectItem.IP, int.Parse(connectItem.Port));
                }

                var _sentinelConn = ConnectionMultiplexer.Connect(configOptions);

                var subscriber = _sentinelConn.GetSubscriber();
                subscriber.Subscribe("message").OnMessage(channelMessage => {
                    Console.WriteLine("redis sub message:" + channelMessage.Message);
                });
                IServer masterServer = GetMasterServer(_sentinelConn);

                Tuple <List <string>, List <string> > masterServersAndSlaverServers = GetMasterServerAndSlavesServer(masterServer, masterServerName);

                var config = ConstructConfigOptions(masterServerName, masterServersAndSlaverServers.Item1, masterServersAndSlaverServers.Item2, password);

                cache.Set(db, config);
            }
            else
            {
                var redisConfiguration = new ConfigurationOptions
                {
                    AbortOnConnectFail = true,
                    AllowAdmin         = false,
                    ConnectRetry       = 5,
                    ConnectTimeout     = 2000,
                    DefaultDatabase    = 0,
                    KeepAlive          = 20,
                    SyncTimeout        = 30 * 1000,
                    Ssl      = false,
                    Password = password
                };

                connections.ForEach(f => redisConfiguration.EndPoints.Add(f.IP, int.Parse(f.Port)));

                var           cm         = cache.Set(db, redisConfiguration);
                var           subscriber = cm.GetSubscriber();
                List <string> sc         = connections.SelectMany(s => s.SubscribChannels).Distinct().ToList() ?? new List <string>();
                sc.ForEach(sf =>
                {
                    subscriber.Subscribe(sf).OnMessage(channelMessage =>
                    {
                        if (handle != null)
                        {
                            handle(sf, channelMessage.Message);
                        }
                        else
                        {
                            Console.WriteLine($"CHANNEL[{sf}]-MESSAGE : NOT FOUND AN ACTION TO HANDLE IT");
                        }
                    });
                });
            }

            return(cache);
        }