StackExchange.Redis 之 String 类型示例

String类型很简单,就不做示例演示了,这里只贴出Helper类

        /// 
        /// 判断key是否存在
        /// 
        /// 
        /// 
        /// 
        public bool KeyExists(string key, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                return KeyExists(key);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// 
        /// 根据key 移除缓存  
        /// 
        /// 
        /// 
        public bool Remove(string key, int db = -1)
        {
            try
            {
                if ((object)key == null)
                    return false;
                _db = GetDatabase(db);
                return KeyDelete(key);
            }
            catch (Exception ex)
            {
                _log.Fatal("移除:{0}异常:{1}", key, ex.Message);
                throw;
            }
        }


        /// 
        /// 写入缓存数据
        /// 
        /// 类型
        /// 
        /// 
        /// 有效期
        /// 
        public bool Set(string key, T value, TimeSpan span, int db = -1)
        {
            try
            {
                if ((object)key == null || value == null)
                    return false;

                _db = GetDatabase(db);
                if (value != null && (object)value is string)
                {
                    return StringSet(key, value.ToString(), span);
                }
                return StringSet(key, value, span);
            }
            catch (Exception ex)
            {
                _log.Fatal("{0}key写入value:{1}失败,异常原因:{2}", key, value, ex.Message);
                throw;
            }
        }

        /// 
        /// 读取缓存数据  返回实体
        /// 
        /// 
        /// 
        /// 
        public T Get(string key, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                return StringGet(key);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// 
        /// 读取缓存数据 返回字符串
        /// 
        /// 
        /// 
        public string Get(string key, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                return StringGet(key);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// 
        /// 递增
        /// 
        /// 
        /// 
        /// 
        /// 
        public long Increment(string key, TimeSpan span, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var result = this.StringIncr(key);
                //设置过期时间
                this.KeyExpire(key, span);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// 
        /// 递减
        /// 
        /// 
        /// 
        /// 
        /// 
        public long Decrement(string key, TimeSpan span, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var result = this.StringDecr(key);
                //设置过期时间
                this.KeyExpire(key, span);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }

 

你可能感兴趣的:(StackExchange.Redis 之 String 类型示例)