在用C#连接SQL数据库时,我们需要在Config配置文件里面配置SQL连接字符串,同样,在连接Redis时,也可以配置连接字符串:

="RedisTest"  WriteServer="127.0.0.1:6379,password=123456,connectTimeout=1000,connectRetry=1
,syncTimeout=1000"/>

注释:

  • 127.0.0.1:6379:IP,端口
  • password:Redis密码
  • connectTimeout:连接超时时间,这里设置的是1000毫秒
  • connectRetry:重试连接次数
  • syncTimeout:同步操作默认超时时间

附上RedisHelper源代码(需添加StackExchange.Redis引用)

  #region Redis帮助类
    /// 
    /// Redis帮助类
    /// 
    public class RedisHelper
    {
        /// 
        /// 连接字符串,一般写在配置文件里面
        /// 
        private static readonly string ConnectionString = "127.0.0.1:6379,password=123456,connectTimeout=1000,connectRetry=1,syncTimeout=1000";
        /// 
        /// 上锁,单例模式
        /// 
        private static object locker = new object();
        /// 
        /// 连接对象
        /// 
        private volatile IConnectionMultiplexer _connection;
        /// 
        /// 数据库
        /// 
        private IDatabase _db;
        #region 创建Redis实例
        public RedisHelper()
        {
            _connection = ConnectionMultiplexer.Connect(ConnectionString);
            _db = GetDatabase();
        }

        private static RedisHelper redisHelper;

        public static RedisHelper GetRedisHelper()
        {

            if (redisHelper == null)
            {
                lock (locker)
                {
                    if (redisHelper == null)
                    {
                        redisHelper = new RedisHelper();
                    }
                }
            }
            return redisHelper;
        }
        #endregion


        /// 
        /// 获取连接
        /// 
        /// 
        protected IConnectionMultiplexer GetConnection()
        {
            if (_connection != null && _connection.IsConnected)
            {
                return _connection;
            }
            lock (locker)
            {
                if (_connection != null && _connection.IsConnected)
                {
                    return _connection;
                }

                if (_connection != null)
                {
                    _connection.Dispose();
                }
                _connection = ConnectionMultiplexer.Connect(ConnectionString);
            }

            return _connection;
        }
        /// 
        /// 获取数据库
        /// 
        /// 
        /// 
        public IDatabase GetDatabase(int? db = null)
        {
            return GetConnection().GetDatabase(db ?? -1);
        }
        /// 
        /// 设置
        /// 
        /// 键
        /// 值
        /// 过期时间
        public virtual void Set(string key, object data, int? cacheTime=null)
        {
            if (data == null)
            {
                return;
            }
            var entryBytes = Serialize(data);
            if (cacheTime != null)
            {
                var expiresIn = TimeSpan.FromMinutes(Convert.ToDouble(cacheTime));
                _db.StringSet(key, entryBytes, expiresIn);
            }
            else
            {
                _db.StringSet(key, entryBytes);
            }

        }

        /// 
        /// 根据键获取值
        /// 
        /// 
        /// 
        /// 
        public virtual T Get(string key)
        {
            var rValue = _db.StringGet(key);
            if (!rValue.HasValue)
            {
                return default(T);
            }

            var result = Deserialize(rValue);

            return result;
        }

        /// 
        /// 判断键是否已存在
        /// 
        /// 
        /// 
        public bool IsExit(string key)
        {
            return _db.KeyExists(key);
        }

        /// 
        /// 判断是否已经设置
        /// 
        /// 
        /// 
        public virtual bool IsSet(string key)
        {
            return _db.KeyExists(key);
        }
        /// 
        /// 序列化
        /// 
        /// 
        /// byte[]
        private byte[] Serialize(object data)
        {
            var json = JsonConvert.SerializeObject(data);
            return Encoding.UTF8.GetBytes(json);
        }

        /// 
        /// 反序列化
        /// 
        /// 
        /// 
        /// 
        protected virtual T Deserialize(byte[] serializedObject)
        {
            if (serializedObject == null)
            {
                return default(T);
            }
            var json = Encoding.UTF8.GetString(serializedObject);
            return JsonConvert.DeserializeObject(json);
        }
    }
    #endregion
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐