IRedisHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using StackExchange.Redis;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace OASystem.RedisRepository.RedisAsyncHelper
  8. {
  9. /// <summary>
  10. /// 异步方法接口
  11. /// 存入数据均为方法内部序列化后的byte,所以取数据的时候需要反序列化时,请指定正确的数据类型
  12. /// </summary>
  13. public partial interface IRedisHelper
  14. {
  15. #region Redis数据类型—String
  16. /// <summary>
  17. /// 将任何数据以redis string存储
  18. /// </summary>
  19. /// <typeparam name="T"></typeparam>
  20. /// <param name="key"></param>
  21. /// <param name="value"></param>
  22. /// <param name="timeout"></param>
  23. /// <returns></returns>
  24. Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? timeout = null);
  25. /// <summary>
  26. /// 对数值进行减法操作,默认-1
  27. /// </summary>
  28. /// <param name="key"></param>
  29. /// <param name="value"></param>
  30. /// <returns>操作后的结果</returns>
  31. Task<long> StringDecrementAsync(string key, long value = 1L);
  32. /// <summary>
  33. /// 对数值进行加法操作,默认+1
  34. /// </summary>
  35. /// <param name="key"></param>
  36. /// <param name="value"></param>
  37. /// <returns>操作后的结果</returns>
  38. Task<long> StringIncrementAsync(string key, long value = 1L);
  39. /// <summary>
  40. /// 从redis string中以指定类型取出
  41. /// </summary>
  42. /// <typeparam name="T"></typeparam>
  43. /// <param name="key"></param>
  44. /// <returns></returns>
  45. Task<T> StringGetAsync<T>(string key);
  46. #endregion
  47. #region Redis数据类型—Hash
  48. /// <summary>
  49. /// 向Hash key中存储任意类型任意值
  50. /// </summary>
  51. /// <typeparam name="T"></typeparam>
  52. /// <param name="key"></param>
  53. /// <param name="field"></param>
  54. /// <param name="value"></param>
  55. /// <returns>是否成功</returns>
  56. Task<bool> HashSetAsync<T>(string key, string field, T value);
  57. /// <summary>
  58. /// 批量 向Hash key中存储任意类型任意值
  59. /// </summary>
  60. /// <typeparam name="T"></typeparam>
  61. /// <param name="key"></param>
  62. /// <param name="hashFields"></param>
  63. /// <returns>无返回值</returns>
  64. Task HashMultiSetAsync<T>(string key, Dictionary<string, T> hashFields);
  65. /// <summary>
  66. /// 对指定hash key中制定field做数量增加操作 默认自增1
  67. /// 如果此操作前key不存在 则创建。 如果此操作前该field不存在或者非数字 则先被置0,再被继续操作
  68. /// </summary>
  69. /// <param name="key"></param>
  70. /// <param name="field"></param>
  71. /// <param name="incrCount"></param>
  72. /// <returns>操作后的结果</returns>
  73. Task<long> HashIncrementAsync(string key, string field, long incrCount = 1);
  74. /// <summary>
  75. /// 对指定hash key中制定field做数量增加操作 默认自减1
  76. /// 如果此操作前key不存在 则创建。 如果此操作前该field不存在或者非数字 则先被置0,再被继续操作
  77. /// </summary>
  78. /// <param name="key"></param>
  79. /// <param name="field"></param>
  80. /// <param name="decrCount"></param>
  81. /// <returns>操作后的结果</returns>
  82. Task<long> HashDecrementAsync(string key, string field, long decrCount = 1);
  83. /// <summary>
  84. /// 从指定Hash中 删除指定field
  85. /// 如果key或者field不存在,则false
  86. /// </summary>
  87. /// <param name="key"></param>
  88. /// <param name="field"></param>
  89. /// <returns>是否成功</returns>
  90. Task<bool> HashDeleteFieldAsync(string key, string field);
  91. /// <summary>
  92. /// 从指定Hash key中 批量删除指定field
  93. /// 如果key或者field不存在,则false
  94. /// </summary>
  95. /// <param name="key"></param>
  96. /// <param name="fields"></param>
  97. /// <returns>移除数量</returns>
  98. Task<long> HashMultiDeleteFieldAsync(string key, List<string> fields);
  99. /// <summary>
  100. /// 从指定Hash key中获取指定field值
  101. /// </summary>
  102. /// <typeparam name="T"></typeparam>
  103. /// <param name="key"></param>
  104. /// <param name="field"></param>
  105. /// <returns></returns>
  106. Task<T> HashGetAsync<T>(string key, string field);
  107. /// <summary>
  108. /// 从指定Hash key中判断field是否存在
  109. /// </summary>
  110. /// <param name="key"></param>
  111. /// <param name="field"></param>
  112. /// <returns></returns>
  113. Task<bool> HashFieldExistAsync(string key, string field);
  114. /// <summary>
  115. /// 获取指定Hash key中的所有field的值
  116. /// </summary>
  117. /// <typeparam name="T"></typeparam>
  118. /// <param name="key"></param>
  119. /// <returns></returns>
  120. Task<List<T>> HashValuesAsync<T>(string key);
  121. /// <summary>
  122. /// 获取指定Hash key中所有 field名称及其Value
  123. /// </summary>
  124. /// <typeparam name="T"></typeparam>
  125. /// <param name="key"></param>
  126. /// <returns></returns>
  127. Task<Dictionary<string, T>> HashGetAllAsync<T>(string key);
  128. /// <summary>
  129. /// 获取指定Hash key中所有field
  130. /// </summary>
  131. /// <param name="key"></param>
  132. /// <returns></returns>
  133. Task<List<string>> HashFieldsAsync(string key);
  134. #endregion
  135. #region Redis数据类型—List
  136. /// <summary>
  137. /// 在指定pivot后插入value, 如果pivot不存在,则返回-1, 如果key不存在,则返回0
  138. /// 如果存在多个相同指定的的pivot,则插入第一个指定pivot后面.
  139. /// 即链表从左向右查找,遇到指定pivot,则确定位置
  140. /// </summary>
  141. /// <typeparam name="T"></typeparam>
  142. /// <param name="key"></param>
  143. /// <param name="pivot">list中的一个值</param>
  144. /// <param name="value"></param>
  145. /// <returns></returns>
  146. Task<long> ListInsertAfterAsync<T>(string key, string pivot, T value);
  147. /// <summary>
  148. /// 在指定pivot前插入value, 如果pivot不存在,则返回-1, 如果key不存在,则返回0
  149. /// 如果存在多个相同指定的的pivot,则插入第一个指定pivot前面.
  150. /// 即链表从左向右查找,遇到指定pivot,则确定位置
  151. /// </summary>
  152. /// <typeparam name="T"></typeparam>
  153. /// <param name="key"></param>
  154. /// <param name="pivot"></param>
  155. /// <param name="value"></param>
  156. /// <returns></returns>
  157. Task<long> ListInsertBeforeAsync<T>(string key, string pivot, T value);
  158. /// <summary>
  159. /// 从链表左侧弹出第一个元素(弹出能获取到该元素并且被删除)
  160. /// 如果key不存在 或者链表为空 则为null
  161. /// </summary>
  162. /// <typeparam name="T"></typeparam>
  163. /// <param name="key"></param>
  164. /// <returns></returns>
  165. Task<T> ListLeftPopAsync<T>(string key);
  166. /// <summary>
  167. /// 从链表左侧增加一个元素,key不存在则被创建
  168. /// </summary>
  169. /// <typeparam name="T"></typeparam>
  170. /// <param name="key"></param>
  171. /// <param name="value"></param>
  172. /// <returns>返回操作后的链表长度</returns>
  173. Task<long> ListLeftPushAsync<T>(string key, T value);
  174. /// <summary>
  175. /// 从链表左侧批量增加元素,如果 a b c 则c会在链表左侧第一位 b第二位 a第三位
  176. /// </summary>
  177. /// <typeparam name="T"></typeparam>
  178. /// <param name="key"></param>
  179. /// <param name="values"></param>
  180. /// <returns>返回操作后的链表长度</returns>
  181. Task<long> ListLeftMultiPushAsync<T>(string key, List<T> values);
  182. /// <summary>
  183. /// 获取链表长度,不存在key则为0
  184. /// </summary>
  185. /// <typeparam name="T"></typeparam>
  186. /// <param name="key"></param>
  187. /// <returns></returns>
  188. Task<long> ListLengthAsync<T>(string key);
  189. /// <summary>
  190. /// 获取链表中所有数据,从左侧start开始到stop结束,从0—-1则认为获取全部,默认获取全部
  191. /// start为负数则代表从链表右侧开始,-1为右侧第一位,-2为右侧第二位
  192. /// start要小于stop,否则返回null
  193. /// </summary>
  194. /// <typeparam name="T"></typeparam>
  195. /// <param name="key"></param>
  196. /// <param name="start"></param>
  197. /// <param name="stop"></param>
  198. /// <returns></returns>
  199. Task<List<T>> ListRangeAsync<T>(string key, long start = 0L, long stop = -1L);
  200. /// <summary>
  201. /// 从链表中一处count数量的value. count大于0则从左至右,count小于0则从右至左,count=0则移除全部
  202. /// </summary>
  203. /// <typeparam name="T"></typeparam>
  204. /// <param name="key"></param>
  205. /// <param name="value"></param>
  206. /// <param name="count"></param>
  207. /// <returns></returns>
  208. Task<long> ListRemoveAsync<T>(string key, T value, long count = 0L);
  209. /// <summary>
  210. /// 从右侧弹出第一个元素(弹出能获取到该元素并且被删除)
  211. /// </summary>
  212. /// <typeparam name="T"></typeparam>
  213. /// <param name="key"></param>
  214. /// <returns></returns>
  215. Task<T> ListRightPopAsync<T>(string key);
  216. /// <summary>
  217. /// 从链表右侧加入元素,如果 rpush a b c 则c为右侧第一位 b第二位 c第三位
  218. /// </summary>
  219. /// <typeparam name="T"></typeparam>
  220. /// <param name="key"></param>
  221. /// <param name="value"></param>
  222. /// <returns></returns>
  223. Task<long> ListRightPushAsync<T>(string key, T value);
  224. /// <summary>
  225. /// 从右侧批量插入,和左侧相反
  226. /// </summary>
  227. /// <typeparam name="T"></typeparam>
  228. /// <param name="key"></param>
  229. /// <param name="values"></param>
  230. /// <returns></returns>
  231. Task<long> ListRightMultiPushAsync<T>(string key, List<T> values);
  232. /// <summary>
  233. /// 在链表指定索引处,插入元素
  234. /// 正数索引从0开始,代表左侧。负数从-1开始 代表从右侧。-1为右侧第一位
  235. /// </summary>
  236. /// <typeparam name="T"></typeparam>
  237. /// <param name="key"></param>
  238. /// <param name="index"></param>
  239. /// <param name="value"></param>
  240. /// <returns></returns>
  241. Task ListSetByIndexAsync<T>(string key, int index, T value);
  242. /// <summary>
  243. /// 留下start到stop之间的数据。负数代表从右侧寻找 -1为右侧第一位
  244. /// </summary>
  245. /// <param name="key"></param>
  246. /// <param name="start"></param>
  247. /// <param name="stop"></param>
  248. /// <returns></returns>
  249. Task ListTrimAsync(string key, long start, long stop);
  250. /// <summary>
  251. /// 获取指定index的值,负数代表从右侧寻找 -1为右侧第一位
  252. /// </summary>
  253. /// <typeparam name="T"></typeparam>
  254. /// <param name="key"></param>
  255. /// <param name="index"></param>
  256. /// <returns></returns>
  257. Task<T> ListGetByIndexAsync<T>(string key, long index);
  258. #endregion
  259. #region Redis数据类型—Set
  260. /// <summary>
  261. /// 向指定集合中增加一个元素
  262. /// </summary>
  263. /// <typeparam name="T"></typeparam>
  264. /// <param name="key"></param>
  265. /// <param name="value"></param>
  266. /// <returns></returns>
  267. Task<bool> SetAddAsync<T>(string key, T value);
  268. ///// <summary>
  269. ///// 指定集合计算操作operation枚举,指定计算结果将存的目标destKey,指定需要参与计算的多个key
  270. ///// </summary>
  271. ///// <param name="operation"></param>
  272. ///// <param name="destKey"></param>
  273. ///// <param name="combineKeys"></param>
  274. ///// <returns></returns>
  275. //Task<long> SetCombineAndStoreAsync(SetOperation operation, string destKey, List<string> combineKeys);
  276. ///// <summary>
  277. ///// 指定集合计算操作operation枚举,指定需要参与计算的多个key
  278. ///// </summary>
  279. ///// <typeparam name="T"></typeparam>
  280. ///// <param name="operation"></param>
  281. ///// <param name="combineKeys"></param>
  282. ///// <returns></returns>
  283. //Task<List<T>> SetCombineAsync<T>(SetOperation operation, List<string> combineKeys);
  284. ///// <summary>
  285. ///// 指定值是否存在于指定集合中
  286. ///// </summary>
  287. ///// <typeparam name="T"></typeparam>
  288. ///// <param name="key"></param>
  289. ///// <param name="value"></param>
  290. ///// <returns></returns>
  291. //Task<bool> SetContainsAsync<T>(string key, T value);
  292. ///// <summary>
  293. ///// 获取指定集合中元素个数
  294. ///// </summary>
  295. ///// <param name="key"></param>
  296. ///// <returns></returns>
  297. //Task<long> SetLengthAsync(string key);
  298. ///// <summary>
  299. ///// 获取指定集合中的所有元素
  300. ///// </summary>
  301. ///// <typeparam name="T"></typeparam>
  302. ///// <param name="key"></param>
  303. ///// <param name="value"></param>
  304. ///// <returns></returns>
  305. //Task<List<T>> SetMembersAsync<T>(string key, T value);
  306. ///// <summary>
  307. ///// 从sourceKey移除指定value到目标distKey集合当中
  308. ///// 如果sourceKey存在指定value则返回true,否则不做任何操作返回false
  309. ///// </summary>
  310. ///// <typeparam name="T"></typeparam>
  311. ///// <param name="sourcekey"></param>
  312. ///// <param name="distKey"></param>
  313. ///// <param name="value"></param>
  314. ///// <returns></returns>
  315. //Task<bool> SetMoveAsync<T>(string sourcekey, string distKey, T value);
  316. ///// <summary>
  317. ///// 从指定集合当中随机取出一个元素
  318. ///// </summary>
  319. ///// <typeparam name="T"></typeparam>
  320. ///// <param name="key"></param>
  321. ///// <returns></returns>
  322. //Task<T> SetRandomMemberAsync<T>(string key);
  323. ///// <summary>
  324. ///// 从指定集合随机弹出(删除并获取)一个元素
  325. ///// </summary>
  326. ///// <typeparam name="T"></typeparam>
  327. ///// <param name="key"></param>
  328. ///// <returns></returns>
  329. //Task<T> SetPopAsync<T>(string key);
  330. ///// <summary>
  331. ///// 从集合中随机弹出(删除并获取)多个元素
  332. ///// </summary>
  333. ///// <typeparam name="T"></typeparam>
  334. ///// <param name="key"></param>
  335. ///// <returns></returns>
  336. //Task<List<T>> SetRandomMembersAsync<T>(string key);
  337. ///// <summary>
  338. ///// 从集合中移除指定元素
  339. ///// </summary>
  340. ///// <typeparam name="T"></typeparam>
  341. ///// <param name="key"></param>
  342. ///// <param name="value"></param>
  343. ///// <returns></returns>
  344. //Task<bool> SetRemoveAsync<T>(string key, T value);
  345. ///// <summary>
  346. ///// 从集合中批量移除元素
  347. ///// </summary>
  348. ///// <typeparam name="T"></typeparam>
  349. ///// <param name="key"></param>
  350. ///// <param name="values"></param>
  351. ///// <returns></returns>
  352. //Task<long> SetMultiRemoveAsync<T>(string key, List<T> values);
  353. #endregion
  354. #region Redis数据类型—SortSet
  355. #endregion
  356. #region Redis Key操作
  357. /// <summary>
  358. /// 删除指定key
  359. /// </summary>
  360. /// <param name="key"></param>
  361. /// <returns></returns>
  362. Task<bool> KeyDeleteAsync(string key);
  363. /// <summary>
  364. /// 设置key过期时间具体DateTime
  365. /// </summary>
  366. /// <param name="key"></param>
  367. /// <param name="expireAt"></param>
  368. /// <returns></returns>
  369. //Task<bool> KeyExpireAtAsync(string key, DateTime expireAt);
  370. /// <summary>
  371. /// 设置key在将来的timeout后过期(TimeSpan)
  372. /// </summary>
  373. /// <param name="key"></param>
  374. /// <param name="timeout"></param>
  375. /// <returns></returns>
  376. Task<bool> KeyExpireInAsync(string key, TimeSpan timeout);
  377. /// <summary>
  378. /// key重命名
  379. /// </summary>
  380. /// <param name="key"></param>
  381. /// <param name="newKey"></param>
  382. /// <returns></returns>
  383. Task<bool> KeyRenameAsync(string key, string newKey);
  384. /// <summary>
  385. /// 判断key是否已存在
  386. /// </summary>
  387. /// <param name="key"></param>
  388. /// <returns></returns>
  389. Task<bool> KeyExistsAsync(string key);
  390. #endregion
  391. #region Redis Transcation
  392. /// <summary>
  393. /// 在事务中执行一系列redis命令。注意:在委托中的一系列命令的所有 值 都需要进行字节数组序列化
  394. /// </summary>
  395. /// <param name="ranOperations"></param>
  396. /// <returns></returns>
  397. //Task<bool> DoInTranscationAsync(Action<ITransaction> ranOperations);
  398. #endregion
  399. }
  400. }