示例#1
0
        /// <summary>
        /// 获取Redis连接对象(线程安全)
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static IConnectionMultiplexer GetRedisClient(RedisClientConfigurations configurations)
        {
            var connectionString = string.Format(DefaultConnectionStringFormat,
                                                 string.Join(",", configurations.Url),
                                                 configurations.ConnectTimeout,
                                                 configurations.ConnectRetry);

            if (!ConnectionCache.ContainsKey(connectionString))
            {
                ConnectionCache[connectionString] = GetConnectionRedisMultiplexer(connectionString);
            }
            return(ConnectionCache[connectionString]);
        }
示例#2
0
        /// <summary>
        /// 创建redis链接
        /// </summary>
        /// <param name="dbNum"></param>
        /// <param name="defaultKey"></param>
        /// <param name="redisUri"></param>
        /// <returns></returns>
        public static IDatabase GetDatabase(int dbNum = 0, string redisUri = "")
        {
            var config = new RedisClientConfigurations {
                Url = redisUri
            };
            var client = RedisClient.GetRedisClient(config);

            if (client.IsConnected)
            {
                return(client.GetDatabase(db: dbNum));
            }
            else
            {
                throw new ArgumentException("RedisHelper.client", $"Redis未能成功连接。连接配置为{config.ToJson()}");
            }
        }
示例#3
0
        /// <summary>
        /// 创建redis订阅链接
        /// </summary>
        /// <param name="redisUri"></param>
        /// <returns></returns>
        public static ISubscriber GetSubscriber(string redisUri = "")
        {
            var config = new RedisClientConfigurations {
                Url = redisUri
            };
            var client = RedisClient.GetRedisClient(config);

            if (client.IsConnected)
            {
                client.PreserveAsyncOrder = config.PreserveAsyncOrder;
                return(client.GetSubscriber());
            }
            else
            {
                throw new ArgumentException("RedisHelper.client", $"Redis订阅未能成功连接。连接配置为{config.ToJson()}");
            }
        }