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; } }