Exemple #1
0
 public RedisDB(AppDBNameEnum dbNameEnum, int index, IRedisServer server)
 {
     this.m_Name     = dbNameEnum.ToString();
     this.m_Index    = index;
     this.m_Server   = server;
     this.m_DataBase = server.GetDB(index);
 }
        private void SubscribeToGroupManagementChannel(IRedisServer server)
        => server.Subscriber.Subscribe(_channels.GroupManagement, async(_, data) =>
        {
            try
            {
                var groupMessage = _protocol.ReadGroupCommand((byte[])data);

                var connection = _connections[groupMessage.ConnectionId];
                if (connection == null)
                {
                    // user not on this server
                    return;
                }

                if (groupMessage.Action == GroupAction.Remove)
                {
                    await RemoveGroupAsyncCore(connection, groupMessage.GroupName);
                }

                if (groupMessage.Action == GroupAction.Add)
                {
                    await AddGroupAsyncCore(connection, groupMessage.GroupName);
                }

                // Send an ack to the server that sent the original command.
                await PublishAsync(server, _channels.Ack(groupMessage.ServerName), _protocol.WriteAck(groupMessage.Id));
            }
            catch (Exception ex)
            {
                RedisLog.InternalMessageFailed(_logger, ex);
            }
        });
        private void SubscribeToAll(IRedisServer server)
        {
            if (!server.IsDedicatedForAllChannel)
            {
                return;
            }

            RedisLog.Subscribing(_logger, _channels.All);
            server.Subscriber.Subscribe(_channels.All, async(_, data) =>
            {
                try
                {
                    RedisLog.ReceivedFromChannel(_logger, _channels.All);
                    var invocation = _protocol.ReadInvocation((byte[])data);
                    var tasks      = _connections.AsEnumerable()
                                     .Where(connectionContext =>
                    {
                        if (invocation.ExcludedConnectionIds == null)
                        {
                            return(true);
                        }

                        return(!invocation.ExcludedConnectionIds.Contains(connectionContext.ConnectionId));
                    })
                                     .Select(connectionContext => connectionContext.WriteAsync(invocation.Message).AsTask());

                    await Task.WhenAll(tasks);
                }
                catch (Exception ex)
                {
                    RedisLog.FailedWritingMessage(_logger, ex);
                }
            });
        }
        private async Task PublishAsync(IRedisServer server, string channel, byte[] payload)
        {
            await EnsureRedisServerConnection();

            RedisLog.PublishToChannel(_logger, channel);
            await server.Subscriber.PublishAsync(channel, payload);
        }
        /// <summary>
        /// Create server specific channel in order to send an ack to a single server
        /// </summary>
        /// <param name="server"></param>
        private void SubscribeToAckChannel(IRedisServer server)
        {
            var ackChannel = _channels.Ack(server.ServerName);

            server.Subscriber.Subscribe(ackChannel, (_, data) =>
            {
                var ackId = _protocol.ReadAck((byte[])data);
                _ackHandler.TriggerAck(ackId);
            });
        }
 public YoungoManager(IRedisServer IRedisServer, IOptions <WebConfig> WebConfig)
 {
     _IRedisServer      = IRedisServer;
     _WebConfig         = WebConfig;
     _IRedisServer.conn = _WebConfig.Value.IsTest ? _WebConfig.Value.TestRedisHost : _WebConfig.Value.RedisHost;
     Api_Host           = _WebConfig.Value.IsTest ? _WebConfig.Value.TestApiHost : _WebConfig.Value.ApiHost;
     //LogsContext lc = new LogsContext(options);
     //_CacheProductListService = new CacheProductListService(lc);
     //Api_Host = "http://localhost:8001/";//测试使用
     //Api_Host = "https://api.starmile.com.cn/";//正式环境
 }
        public void RedisServerTestsSetUp()
        {
            _serverMock = new Mock <IServer>();
            _serverMock.Setup(x => x.ConfigGet(It.IsAny <RedisValue>(), It.IsAny <CommandFlags>()))
            .Returns(new[] { new KeyValuePair <string, string>("config1", "abcdefgh") });
            _serverMock.Setup(x => x.LastSave(It.IsAny <CommandFlags>()))
            .Returns(new DateTime(2014, 12, 3));
            _serverMock.Setup(x => x.LastSaveAsync(It.IsAny <CommandFlags>()))
            .Returns(Task.FromResult(new DateTime(2014, 12, 3)));
            _serverMock.Setup(x => x.Time(It.IsAny <CommandFlags>()))
            .Returns(new DateTime(2014, 12, 3, 11, 23, 12));
            _serverMock.Setup(x => x.TimeAsync(It.IsAny <CommandFlags>()))
            .Returns(Task.FromResult(new DateTime(2014, 12, 3, 11, 23, 12)));
            _serverMock.Setup(x => x.InfoAsync(It.IsAny <RedisValue>(), It.IsAny <CommandFlags>()))
            .Returns(Task.FromResult <IGrouping <string, KeyValuePair <string, string> >[]>(null));
            _serverMock.Setup(x => x.ConfigGetAsync(It.IsAny <RedisValue>(), It.IsAny <CommandFlags>()))
            .Returns(Task.FromResult(new[] { new KeyValuePair <string, string>("config1", "abcdefgh") }));

            _serverMock.Setup(x => x.ConfigSetAsync(It.IsAny <RedisValue>(), It.IsAny <RedisValue>(), It.IsAny <CommandFlags>()))
            .Returns(Task.CompletedTask);
            _serverMock.Setup(x => x.SaveAsync(It.IsAny <SaveType>(), It.IsAny <CommandFlags>()))
            .Returns(Task.CompletedTask);
            _serverMock.Setup(x => x.EndPoint)
            .Returns(new DnsEndPoint("localhost", 6379));
            _serverMock.Setup(x => x.IsConnected)
            .Returns(true);
            _serverMock.Setup(x => x.ServerType)
            .Returns(ServerType.Cluster);
            _serverMock.Setup(x => x.Version)
            .Returns(new Version(1, 0, 2345, 21344));
            _serverMock.Setup(x => x.IsSlave)
            .Returns(true);

            _serverMock.Setup(x => x.FlushAllDatabases(It.IsAny <CommandFlags>()));
            _serverMock.Setup(x => x.FlushDatabase(It.IsAny <int>(), It.IsAny <CommandFlags>()));
            _serverMock.Setup(x => x.FlushAllDatabasesAsync(It.IsAny <CommandFlags>()))
            .Returns(Task.FromResult <object>(null));
            _serverMock.Setup(x => x.FlushDatabaseAsync(It.IsAny <int>(), It.IsAny <CommandFlags>()))
            .Returns(Task.FromResult <object>(null));

            _serverMock
            .Setup(x => x.Keys(It.IsAny <int>(), It.IsAny <RedisValue>(), It.IsAny <int>(), It.IsAny <CommandFlags>()))
            .Returns(new List <RedisKey> {
                "somePrefixkey1", "somePrefixkey2"
            });

            _redisServer = new RedisServer(_serverMock.Object, new NullLogger <RedisServer>(), "somePrefix");
        }
        private async Task EnsureRedisServerConnection()
        {
            if (_defaultServer == null || _shardingServers == null)
            {
                await _connectionLock.WaitAsync();

                try
                {
                    if (_defaultServer == null || _shardingServers == null)
                    {
                        var writer = new LoggerTextWriter(_logger);

                        var tasks = _options.Configurations.Select(async configuration =>
                        {
                            var serverName = _options.ServerNameGenerator.GenerateServerName();
                            RedisLog.ConnectingToEndpoints(_logger, configuration.Options.EndPoints, serverName);
                            var redisConnection = await _options.ConnectAsync(configuration.Options, writer);
                            var server          = new ShardingRedisServer(serverName, configuration.IsDedicatedForAllChannel, redisConnection, _logger);

                            SubscribeToGroupManagementChannel(server);
                            SubscribeToAckChannel(server);

                            return(server);
                        }).ToArray();

                        var redisServers = await Task.WhenAll(tasks);

                        _defaultServer = redisServers.FirstOrDefault(server => server.IsDedicatedForAllChannel)
                                         ?? redisServers.First();

                        SubscribeToAll(_defaultServer);

                        var shardingServers = redisServers.Where(server => !server.IsDedicatedForAllChannel);

                        _shardingServers = shardingServers.ToArray();
                    }
                }
                finally
                {
                    _connectionLock.Release();
                }
            }
        }
 public SilentRedisServer(IRedisServer redisServer, ILogger <SilentRedisServer> logger)
 {
     _redisServer = redisServer;
     _logger      = logger;
 }
 public SilentRedisServer(IRedisServer redisServer)
 {
     _redisServer = redisServer;
 }
 public KeySpaceNotificationService(IRedisServer redisServer, IRedisConfiguration redisConfiguration)
 {
     _redisServer        = redisServer;
     _redisConfiguration = redisConfiguration;
 }
 public RedisCacheManager(IRedisServer redisServer)
 {
     _redisServer = redisServer;
 }