Beispiel #1
0
        public PooledRedisLite(string host, int port = 6379, string password = null, int db = 0, int poolSize = 20)
        {
            Host     = host ?? throw new ArgumentNullException(nameof(host));
            Port     = port;
            PoolSize = poolSize;
            Db       = db;
            Password = password;

            // Init clients
            _writeClients    = new RedisLite[PoolSize];
            _readOnlyClients = new RedisLite[PoolSize];
            for (var i = 0; i < poolSize; i++)
            {
                _writeClients[i]    = new RedisLite(host, port, Password, db);
                _readOnlyClients[i] = new RedisLite(host, port, Password, db);
            }
        }
Beispiel #2
0
        private RedisLite GetInActiveRedisClient(bool readOnly = false)
        {
            var desiredIndex = (readOnly ? _readOnlyClientIndex : _writeClientIndex) % PoolSize;
            var clients      = readOnly ? _readOnlyClients : _writeClients;

            for (var i = desiredIndex; i < desiredIndex + clients.Length; i++)
            {
                var index = i % PoolSize;

                if (readOnly)
                {
                    _readOnlyClientIndex = index + 1;
                }
                else
                {
                    _writeClientIndex = index + 1;
                }

                if (clients[index] != null && !clients[index].Active && !clients[index].HadExceptions)
                {
                    return(clients[index]);
                }

                if (clients[index] == null || clients[index].HadExceptions)
                {
                    if (clients[index] != null)
                    {
                        clients[index].Dispose();
                    }
                    var client = new RedisLite(Host, Port, Password, Db);

                    clients[index] = client;
                    return(client);
                }
            }

            return(null);
        }