String類型很簡單,就不做示例演示了,這裡只貼出Helper類 /// <summary> /// 判斷key是否存在 /// </summary> /// <param name="key"></param> /// <param name="db"></param> /// <returns ...
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; } }