static RedisFactory() { CacheSetting cacheSetting = CacheSetting.GetInstance(); if (cacheSetting != null) { var connectionPools = new CustomShardedConnectionPool[cacheSetting.Pools.Count]; int index = 0; foreach (CacheSetting.PoolConfig poolConfig in cacheSetting.Pools) { long initalDb = poolConfig.Db ?? 0L; RedisClientManagerConfig config = poolConfig.MaxReadPoolSize.HasValue || poolConfig.MaxWritePoolSize.HasValue?new RedisClientManagerConfig():null; if (config != null) { config.MaxReadPoolSize = poolConfig.MaxReadPoolSize.HasValue ? poolConfig.MaxReadPoolSize.Value : 50; config.MaxWritePoolSize = poolConfig.MaxWritePoolSize.HasValue ? poolConfig.MaxWritePoolSize.Value : 50; } var pool = new CustomShardedConnectionPool(poolConfig.Name, poolConfig.Weight, config, initalDb, poolConfig.Hosts); pool.PoolTimeout = poolConfig.PoolTimeout ?? 1000; pool.ConnectTimeout = poolConfig.ConnectTimeout; pool.SocketSendTimeout = poolConfig.SocketSendTimeout; pool.SocketReceiveTimeout = poolConfig.SocketReceiveTimeout; connectionPools[index] = pool; index++; } RedisClientManager = new AutoDetectShardedRedisClientManager(connectionPools); } }
/// <summary> /// 通过Key映射得到RedisClient对象 /// </summary> /// <param name="key"></param> /// <returns></returns> public static IRedisClient CreateClient(string key) { if (RedisClientManager == null) { return(null); } CustomShardedConnectionPool pool = RedisClientManager.GetConnectionPool(key); //通过key映射到指定的连接池 return(pool != null?pool.GetClient() : null); }