• 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;
                }
            }
  • 相关阅读:
    布局的诡异bug合集+解决方法(更新中)
    java并发:CopyOnWriteArrayList简单理解
    java集合: LinkedList源码浅析
    Idea设置类注释模板
    jquery使用FormData提交数据
    postman发送json请求
    消息队列的简单理解
    如何设计一个消息队列?
    SpringBoot配置logback
    linux下安装kafka
  • 原文地址:https://www.cnblogs.com/peterzhang123/p/12322515.html
Copyright © 2020-2023  润新知