Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

146 lignes
4.5 KiB

  1. using Learun.Cache.Base;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. namespace Learun.Cache.Redis
  6. {
  7. /// <summary>
  8. /// 版 本 V2018.3.28
  9. /// Copyright (c) 2013-2050 上海力软信息技术有限公司
  10. /// 创建人:力软-系统开发组
  11. /// 日 期:2017.03.06
  12. /// 描 述:定义缓存接口
  13. /// 2018.4.6 bertchen 增加Redis 前缀
  14. /// </summary>
  15. public class CacheByRedis : ICache
  16. {
  17. #region Key-Value
  18. /// <summary>
  19. /// 读取缓存
  20. /// </summary>
  21. /// <param name="cacheKey">键</param>
  22. /// <returns></returns>
  23. public T Read<T>(string cacheKey, int dbId = 0) where T : class
  24. {
  25. return new RedisCache(dbId, null).StringGet<T>(cacheKey);
  26. }
  27. /// <summary>
  28. /// 写入缓存
  29. /// </summary>
  30. /// <param name="value">对象数据</param>
  31. /// <param name="cacheKey">键</param>
  32. public void Write<T>(string cacheKey, T value, int dbId = 0) where T : class
  33. {
  34. new RedisCache(dbId, null).StringSet<T>(cacheKey, value);
  35. }
  36. /// <summary>
  37. /// 写入缓存
  38. /// </summary>
  39. /// <param name="value">对象数据</param>
  40. /// <param name="cacheKey">键</param>
  41. /// <param name="expireTime">到期时间</param>
  42. public void Write<T>(string cacheKey, T value, TimeSpan expireTime, int dbId = 0) where T : class
  43. {
  44. new RedisCache(dbId, null).StringSet<T>(cacheKey, value, expireTime);
  45. }
  46. /// <summary>
  47. /// 移除指定数据缓存
  48. /// </summary>
  49. /// <param name="cacheKey">键</param>
  50. public void Remove(string cacheKey, int dbId = 0)
  51. {
  52. new RedisCache(dbId, null).KeyDelete(cacheKey);
  53. }
  54. /// <summary>
  55. /// 移除全部缓存
  56. /// </summary>
  57. public void RemoveAll(int dbId = 0)
  58. {
  59. new RedisCache().FlushDatabase(dbId);
  60. }
  61. #endregion
  62. #region List
  63. #region 同步方法
  64. /// <summary>
  65. /// 移除指定ListId的内部List的值
  66. /// </summary>
  67. /// <param name="key"></param>
  68. /// <param name="value"></param>
  69. public void ListRemove<T>(string cacheKey, T value, int dbId = 0) where T : class
  70. {
  71. new RedisCache(dbId, null).ListRemove<T>(cacheKey, value);
  72. }
  73. /// <summary>
  74. /// 获取指定key的List
  75. /// </summary>
  76. /// <param name="key"></param>
  77. /// <returns></returns>
  78. public List<T> ListRange<T>(string cacheKey, int dbId = 0) where T : class
  79. {
  80. return new RedisCache(dbId, null).ListRange<T>(cacheKey);
  81. }
  82. /// <summary>
  83. /// 入队
  84. /// </summary>
  85. /// <param name="key"></param>
  86. /// <param name="value"></param>
  87. public void ListRightPush<T>(string cacheKey, T value, int dbId = 0) where T : class
  88. {
  89. new RedisCache(dbId, null).ListRightPush(cacheKey, value);
  90. }
  91. /// <summary>
  92. /// 出队
  93. /// </summary>
  94. /// <typeparam name="T"></typeparam>
  95. /// <param name="key"></param>
  96. /// <returns></returns>
  97. public T ListRightPop<T>(string cacheKey, int dbId = 0) where T : class
  98. {
  99. return new RedisCache(dbId, null).ListRightPop<T>(cacheKey);
  100. }
  101. /// <summary>
  102. /// 入栈
  103. /// </summary>
  104. /// <typeparam name="T"></typeparam>
  105. /// <param name="key"></param>
  106. /// <param name="value"></param>
  107. public void ListLeftPush<T>(string cacheKey, T value, int dbId = 0) where T : class
  108. {
  109. new RedisCache(dbId, null).ListLeftPush<T>(cacheKey, value);
  110. }
  111. /// <summary>
  112. /// 出栈
  113. /// </summary>
  114. /// <typeparam name="T"></typeparam>
  115. /// <param name="key"></param>
  116. /// <returns></returns>
  117. public T ListLeftPop<T>(string cacheKey, int dbId = 0) where T : class
  118. {
  119. return new RedisCache(dbId, null).ListLeftPop<T>(cacheKey);
  120. }
  121. /// <summary>
  122. /// 获取集合中的数量
  123. /// </summary>
  124. /// <param name="key"></param>
  125. /// <returns></returns>
  126. public long ListLength(string cacheKey, int dbId = 0)
  127. {
  128. return new RedisCache(dbId, null).ListLength(cacheKey);
  129. }
  130. #endregion 同步方法
  131. #endregion List
  132. }
  133. }