zoukankan      html  css  js  c++  java
  • StackExchange.Redis 之 String 类型示例

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

            /// <summary>
            /// 判断key是否存在
            /// </summary>
            /// <param name="key"></param>
            /// <param name="db"></param>
            /// <returns></returns>
            public bool KeyExists(string key, int db = -1)
            {
                try
                {
                    _db = GetDatabase(db);
                    return KeyExists(key);
                }
                catch (Exception)
                {
                    throw;
                }
            }
    
            /// <summary>
            /// 根据key 移除缓存  
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            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;
                }
            }
    
    
            /// <summary>
            /// 写入缓存数据
            /// </summary>
            /// <typeparam name="T">类型</typeparam>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="span">有效期</param>
            /// <returns></returns>
            public bool Set<T>(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<T>(key, value, span);
                }
                catch (Exception ex)
                {
                    _log.Fatal("{0}key写入value:{1}失败,异常原因:{2}", key, value, ex.Message);
                    throw;
                }
            }
    
            /// <summary>
            /// 读取缓存数据  返回实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="key"></param>
            /// <returns></returns>
            public T Get<T>(string key, int db = -1)
            {
                try
                {
                    _db = GetDatabase(db);
                    return StringGet<T>(key);
                }
                catch (Exception)
                {
                    throw;
                }
            }
    
            /// <summary>
            /// 读取缓存数据 返回字符串
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public string Get(string key, int db = -1)
            {
                try
                {
                    _db = GetDatabase(db);
                    return StringGet(key);
                }
                catch (Exception)
                {
    
                    throw;
                }
            }
    
            /// <summary>
            /// 递增
            /// </summary>
            /// <param name="key"></param>
            /// <param name="span"></param>
            /// <param name="db"></param>
            /// <returns></returns>
            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;
                }
            }
    
            /// <summary>
            /// 递减
            /// </summary>
            /// <param name="key"></param>
            /// <param name="span"></param>
            /// <param name="db"></param>
            /// <returns></returns>
            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;
                }
            }
  • 相关阅读:
    20155302 《信息安全系统设计基础》课程总结
    2017-2018-1 20155302 第十四周作业
    2017-2018-1 20155302 第十三周作业
    2017-2018-1 20155302 实验五 通信协议设计
    20155302实验一
    2017-2018-1 20155302 实验四 外设驱动程序设计
    2017-2018-1 20155302 《信息安全系统设计基础》第11周学习总结
    # 2017-2018-1 20155302 课下实践IPC及课上补充
    2017-2018-1 20155302 实验三 实时系统
    # 2017-2018-1 20155302 《信息安全系统设计基础》第9周学习总结
  • 原文地址:https://www.cnblogs.com/peterzhang123/p/12322515.html
Copyright © 2011-2022 走看看