Beispiel #1
0
 public T GetClient(CacheEndpoint endpoint, int connectTimeout)
 {
     try
     {
         var info = endpoint as RedisEndpoint;
         Check.NotNull(info, "endpoint");
         var key = string.Format("{0}{1}{2}{3}", info.Host, info.Port, info.Password, info.DbIndex);
         if (!_pool.ContainsKey(key))
         {
             var objectPool = new ObjectPool <T>(() =>
             {
                 var point       = string.Format("{0}:{1}", info.Host, info.Port);
                 var redisClient = ConnectionMultiplexer.Connect(new ConfigurationOptions()
                 {
                     EndPoints      = { { point } },
                     ServiceName    = point,
                     Password       = info.Password,
                     ConnectTimeout = connectTimeout
                 });
                 return(redisClient.GetDatabase(info.DbIndex) as T);
             }, info.MinSize, info.MaxSize);
             _pool.GetOrAdd(key, objectPool);
             return(objectPool.GetObject());
         }
         else
         {
             return(_pool[key].GetObject());
         }
     }
     catch (Exception e)
     {
         throw new CacheException(e.Message);
     }
 }
Beispiel #2
0
        public async Task <bool> ConnectionAsync(CacheEndpoint endpoint, int connectTimeout)
        {
            try
            {
                var info  = endpoint as ConsistentHashNode;
                var point = string.Format("{0}:{1}", info.Host, info.Port);
                var conn  = await ConnectionMultiplexer.ConnectAsync(new ConfigurationOptions()
                {
                    EndPoints      = { { point } },
                    ServiceName    = point,
                    Password       = info.Password,
                    ConnectTimeout = connectTimeout
                });

                return(conn.IsConnected);
            }
            catch (Exception e)
            {
                throw new CacheException(e.Message);
            }
        }
Beispiel #3
0
        public async Task <bool> ConnectionAsync(CacheEndpoint endpoint)
        {
            var connection = await _cacheClient.ConnectionAsync(endpoint, _connectTimeout);

            return(connection);
        }
Beispiel #4
0
 private IDatabase GetRedisClient(CacheEndpoint info)
 {
     return(_cacheClient.GetClient(info, _connectTimeout));
 }