平安校园
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.

ISimpleCacheService.cs 6.4 KiB

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