/// <summary>
        /// Returns a ReadOnly client using the hosts defined in ReadOnlyHosts.
        /// </summary>
        /// <returns></returns>
        public virtual IRedisClient GetReadOnlyClient()
        {
            var nextHost = ReadOnlyHosts[readOnlyHostsIndex++ % ReadOnlyHosts.Count];
            var client   = RedisClientFactory.CreateRedisClient(
                nextHost.Host, nextHost.Port);

            if (this.ConnectTimeout != null)
            {
                client.ConnectTimeout = this.ConnectTimeout.Value;
            }

            //Set database to userSpecified if different
            if (Db != RedisNativeClient.DefaultDb)
            {
                client.ChangeDb(Db);
            }

            if (nextHost.RequiresAuth)
            {
                client.Password = nextHost.Password;
            }

            client.NamespacePrefix  = NamespacePrefix;
            client.ConnectionFilter = ConnectionFilter;

            return(client);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Called within a lock
        /// </summary>
        /// <returns></returns>
        private RedisClient GetInActiveReadClient()
        {
            for (var i = 0; i < readClients.Length; i++)
            {
                var nextIndex = (ReadPoolIndex + i) % readClients.Length;

                //Initialize if not exists
                var existingClient = readClients[nextIndex];
                if (existingClient == null ||
                    existingClient.HadExceptions)
                {
                    if (existingClient != null)
                    {
                        existingClient.DisposeConnection();
                    }

                    var nextHost = ReadOnlyHosts[nextIndex % ReadOnlyHosts.Count];
                    var client   = RedisClientFactory.CreateRedisClient(
                        nextHost.Host, nextHost.Port);

                    client.ClientManager = this;

                    readClients[nextIndex] = client;

                    return(client);
                }

                //look for free one
                if (!readClients[nextIndex].Active)
                {
                    return(readClients[nextIndex]);
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns a Read/Write client (The default) using the hosts defined in ReadWriteHosts
        /// </summary>
        /// <returns></returns>
        public IRedisClient GetClient()
        {
            var nextHost = ReadWriteHosts[readWriteHostsIndex++ % ReadWriteHosts.Count];
            var client   = RedisClientFactory.CreateRedisClient(
                nextHost.Host, nextHost.Port);

            //Set database to userSpecified if different
            if (Db != RedisNativeClient.DefaultDb)
            {
                client.Db = Db;
            }

            return(client);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Called within a lock
        /// </summary>
        /// <returns></returns>
        private RedisClient GetInActiveWriteClient()
        {
            var desiredIndex = WritePoolIndex % writeClients.Length;

            //this will loop through all hosts in readClients once even though there are 2 for loops
            //both loops are used to try to get the prefered host according to the round robin algorithm
            for (int x = 0; x < ReadWriteHosts.Count; x++)
            {
                var nextHostIndex = (desiredIndex + x) % ReadWriteHosts.Count;
                var nextHost      = ReadWriteHosts[nextHostIndex];
                for (var i = nextHostIndex; i < writeClients.Length; i += ReadWriteHosts.Count)
                {
                    if (writeClients[i] != null && !writeClients[i].Active && !writeClients[i].HadExceptions)
                    {
                        return(writeClients[i]);
                    }
                    else if (writeClients[i] == null || writeClients[i].HadExceptions)
                    {
                        if (writeClients[i] != null)
                        {
                            writeClients[i].DisposeConnection();
                        }
                        var client = RedisClientFactory.CreateRedisClient(nextHost.Host, nextHost.Port);

                        if (nextHost.RequiresAuth)
                        {
                            client.Password = nextHost.Password;
                        }

                        client.Id               = RedisClientCounter++;
                        client.ClientManager    = this;
                        client.NamespacePrefix  = NamespacePrefix;
                        client.ConnectionFilter = ConnectionFilter;

                        writeClients[i] = client;

                        return(client);
                    }
                }
            }
            return(null);
        }