平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

192 rivejä
6.4 KiB

  1. //
  2. namespace SafeCampus.Cache;
  3. /// <summary>
  4. /// 缓存服务
  5. /// </summary>
  6. public partial interface ISimpleCacheService
  7. {
  8. #region 基础操作
  9. /// <summary>是否包含缓存项</summary>
  10. /// <param name="key"></param>
  11. /// <returns></returns>
  12. bool ContainsKey(string key);
  13. /// <summary>设置缓存项</summary>
  14. /// <param name="key">键</param>
  15. /// <param name="value">值</param>
  16. /// <param name="expire">过期时间,秒。小于0时采用默认缓存时间</param>
  17. /// <returns></returns>
  18. bool Set<T>(string key, T value, int expire = -1);
  19. /// <summary>设置缓存项</summary>
  20. /// <param name="key">键</param>
  21. /// <param name="value">值</param>
  22. /// <param name="expire">过期时间</param>
  23. /// <returns></returns>
  24. bool Set<T>(string key, T value, TimeSpan expire);
  25. /// <summary>获取缓存项</summary>
  26. /// <param name="key">键</param>
  27. /// <returns></returns>
  28. T Get<T>(string key);
  29. /// <summary>批量移除缓存项</summary>
  30. /// <param name="keys">键集合</param>
  31. /// <returns></returns>
  32. int Remove(params string[] keys);
  33. /// <summary>清空所有缓存项</summary>
  34. void Clear();
  35. /// <summary>设置缓存项有效期</summary>
  36. /// <param name="key">键</param>
  37. /// <param name="expire">过期时间</param>
  38. bool SetExpire(string key, TimeSpan expire);
  39. /// <summary>获取缓存项有效期</summary>
  40. /// <param name="key">键</param>
  41. /// <returns></returns>
  42. TimeSpan GetExpire(string key);
  43. /// <summary>
  44. /// 模糊删除
  45. /// </summary>
  46. /// <param name="pattern">匹配关键字</param>
  47. void DelByPattern(string pattern);
  48. #endregion 基础操作
  49. #region 集合操作
  50. /// <summary>批量获取缓存项</summary>
  51. /// <typeparam name="T"></typeparam>
  52. /// <param name="keys"></param>
  53. /// <returns></returns>
  54. IDictionary<string, T> GetAll<T>(IEnumerable<string> keys);
  55. /// <summary>批量设置缓存项</summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="values"></param>
  58. /// <param name="expire">过期时间,秒。小于0时采用默认缓存时间</param>
  59. void SetAll<T>(IDictionary<string, T> values, int expire = -1);
  60. /// <summary>获取列表</summary>
  61. /// <typeparam name="T">元素类型</typeparam>
  62. /// <param name="key">键</param>
  63. /// <returns></returns>
  64. IList<T> GetList<T>(string key);
  65. /// <summary>获取哈希</summary>
  66. /// <typeparam name="T">元素类型</typeparam>
  67. /// <param name="key">键</param>
  68. /// <returns></returns>
  69. IDictionary<string, T> GetDictionary<T>(string key);
  70. /// <summary>获取队列</summary>
  71. /// <typeparam name="T">元素类型</typeparam>
  72. /// <param name="key">键</param>
  73. /// <returns></returns>
  74. IProducerConsumer<T> GetQueue<T>(string key);
  75. /// <summary>获取栈</summary>
  76. /// <typeparam name="T">元素类型</typeparam>
  77. /// <param name="key">键</param>
  78. /// <returns></returns>
  79. IProducerConsumer<T> GetStack<T>(string key);
  80. /// <summary>获取Set</summary>
  81. /// <typeparam name="T"></typeparam>
  82. /// <param name="key"></param>
  83. /// <returns></returns>
  84. ICollection<T> GetSet<T>(string key);
  85. #endregion 集合操作
  86. #region 高级操作
  87. /// <summary>添加,已存在时不更新</summary>
  88. /// <typeparam name="T">值类型</typeparam>
  89. /// <param name="key">键</param>
  90. /// <param name="value">值</param>
  91. /// <param name="expire">过期时间,秒。小于0时采用默认缓存时间</param>
  92. /// <returns></returns>
  93. bool Add<T>(string key, T value, int expire = -1);
  94. /// <summary>设置新值并获取旧值,原子操作</summary>
  95. /// <remarks>
  96. /// 常常配合Increment使用,用于累加到一定数后重置归零,又避免多线程冲突。
  97. /// </remarks>
  98. /// <typeparam name="T">值类型</typeparam>
  99. /// <param name="key">键</param>
  100. /// <param name="value">值</param>
  101. /// <returns></returns>
  102. T Replace<T>(string key, T value);
  103. /// <summary>尝试获取指定键,返回是否包含值。有可能缓存项刚好是默认值,或者只是反序列化失败,解决缓存穿透问题</summary>
  104. /// <typeparam name="T">值类型</typeparam>
  105. /// <param name="key">键</param>
  106. /// <param name="value">值。即使有值也不一定能够返回,可能缓存项刚好是默认值,或者只是反序列化失败</param>
  107. /// <returns>返回是否包含值,即使反序列化失败</returns>
  108. bool TryGetValue<T>(string key, out T value);
  109. /// <summary>累加,原子操作</summary>
  110. /// <param name="key">键</param>
  111. /// <param name="value">变化量</param>
  112. /// <returns></returns>
  113. long Increment(string key, long value);
  114. /// <summary>累加,原子操作</summary>
  115. /// <param name="key">键</param>
  116. /// <param name="value">变化量</param>
  117. /// <returns></returns>
  118. double Increment(string key, double value);
  119. /// <summary>递减,原子操作</summary>
  120. /// <param name="key">键</param>
  121. /// <param name="value">变化量</param>
  122. /// <returns></returns>
  123. long Decrement(string key, long value);
  124. /// <summary>递减,原子操作</summary>
  125. /// <param name="key">键</param>
  126. /// <param name="value">变化量</param>
  127. /// <returns></returns>
  128. double Decrement(string key, double value);
  129. #endregion 高级操作
  130. #region 事务
  131. /// <summary>提交变更。部分提供者需要刷盘</summary>
  132. /// <returns></returns>
  133. int Commit();
  134. /// <summary>申请分布式锁</summary>
  135. /// <param name="key">要锁定的key</param>
  136. /// <param name="msTimeout">锁等待时间,单位毫秒</param>
  137. /// <returns></returns>
  138. IDisposable AcquireLock(string key, int msTimeout);
  139. /// <summary>申请分布式锁</summary>
  140. /// <param name="key">要锁定的key</param>
  141. /// <param name="msTimeout">锁等待时间,申请加锁时如果遇到冲突则等待的最大时间,单位毫秒</param>
  142. /// <param name="msExpire">锁过期时间,超过该时间如果没有主动释放则自动释放锁,必须整数秒,单位毫秒</param>
  143. /// <param name="throwOnFailure">失败时是否抛出异常,如果不抛出异常,可通过返回null得知申请锁失败</param>
  144. /// <returns></returns>
  145. IDisposable AcquireLock(string key, int msTimeout, int msExpire,
  146. bool throwOnFailure);
  147. #endregion 事务
  148. }