StackExchange.Redis 之 String 类型示例
程序员文章站
2022-06-19 23:42:30
String类型很简单,就不做示例演示了,这里只贴出Helper类 /// /// 判断key是否存在 /// /// /// ///
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; } }
上一篇: WebAPI-HTTP编程模型
下一篇: 你最羡慕谁