WMCache.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using CP.Cache.Redis;
  2. using CP.Cache.WebCache;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Diagnostics;
  7. using System.Reflection;
  8. using System.Web;
  9. namespace CP.Cache
  10. {
  11. public class WMCache
  12. {
  13. /// <summary>
  14. /// 本类实体
  15. /// </summary>
  16. private static volatile WMCache instance = null;
  17. /// <summary>
  18. /// cache 接口
  19. /// </summary>
  20. private static ICache mcs;
  21. /// <summary>
  22. /// redis接口
  23. /// </summary>
  24. private static ICache rcs;
  25. /// <summary>
  26. /// 默认700分钟
  27. /// </summary>
  28. public const int CacheTime = 700;
  29. /// <summary>
  30. /// 锁
  31. /// </summary>
  32. private static object lockHelper = new object();
  33. /// <summary>
  34. /// 初始化Cache
  35. /// </summary>
  36. private WMCache()
  37. {
  38. switch (CacheProvider)
  39. {
  40. case 0:
  41. mcs = new WebCacheManager(); //web memory cache
  42. break;
  43. case 1:
  44. rcs = new RedisManager(); //redis cache
  45. mcs = new WebCacheManager(); //web memory cache
  46. break;
  47. default:
  48. mcs = new WebCacheManager(); //web memory cache
  49. break;
  50. }
  51. }
  52. /// <summary>
  53. /// 获取缓存实例
  54. /// </summary>
  55. /// <returns></returns>
  56. public static WMCache GetCacheService()
  57. {
  58. if (instance == null)
  59. {
  60. lock (lockHelper)
  61. {
  62. if (instance == null)
  63. {
  64. instance = new WMCache();
  65. }
  66. }
  67. }
  68. return instance;
  69. }
  70. /// <summary>
  71. /// 添加T到缓存
  72. /// </summary>
  73. /// <typeparam name="T"></typeparam>
  74. /// <param name="key"></param>
  75. /// <param name="o"></param>
  76. /// <param name="t"></param>
  77. /// <param name="IsRedis">是否使用redis</param>
  78. public virtual void AddObject<T>(string key, T o, int t = 60, bool IsRedis = false)
  79. {
  80. if (string.IsNullOrEmpty(key) || o == null)
  81. return;
  82. lock (lockHelper)
  83. {
  84. //HttpContext.Current.Response.Write("SetObject:" + key + "<br/>");
  85. if (IsRedis)
  86. {
  87. rcs.AddObject<T>(key, o, t);
  88. }
  89. else
  90. {
  91. mcs.AddObject<T>(key, o, t);
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// 根据Key获取key值
  97. /// </summary>
  98. /// <typeparam name="T"></typeparam>
  99. /// <param name="key"></param>
  100. /// <returns></returns>
  101. public virtual T GetObject<T>(string key, bool IsRedis = false)
  102. {
  103. //StackTrace ss = new StackTrace(true);
  104. //MethodBase mb = ss.GetFrame(2).GetMethod();
  105. //HttpContext.Current.Response.Write("名称空间:" + mb.DeclaringType.Namespace + ",类名:" + mb.DeclaringType.Name + ",方法名:" + mb.Name + "<br/>");
  106. //HttpContext.Current.Response.Write("Get:" + key + "<br/><br />");
  107. if (string.IsNullOrEmpty(key))
  108. return default(T);
  109. lock (lockHelper)
  110. {
  111. if (IsRedis)
  112. {
  113. return rcs.GetObject<T>(key);
  114. }
  115. else
  116. {
  117. return mcs.GetObject<T>(key);
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// key是否存在...
  123. /// </summary>
  124. /// <param name="key"></param>
  125. /// <param name="IsRedis">是否使用redis</param>
  126. /// <returns></returns>
  127. public virtual bool IsExist(string key, bool IsRedis = false)
  128. {
  129. lock (lockHelper)
  130. {
  131. if (IsRedis)
  132. {
  133. return rcs.IsExist(key);
  134. }
  135. else {
  136. return mcs.IsExist(key);
  137. }
  138. }
  139. }
  140. public virtual List<string> GetKeys(bool IsRedis = false)
  141. {
  142. lock (lockHelper)
  143. {
  144. if (IsRedis)
  145. {
  146. return rcs.GetKeys();
  147. }
  148. else
  149. {
  150. return mcs.GetKeys();
  151. }
  152. }
  153. }
  154. public virtual void RemoveObject(string key)
  155. {
  156. if (string.IsNullOrEmpty(key))
  157. return;
  158. lock (lockHelper)
  159. {
  160. mcs.RemoveObject(key);
  161. rcs?.RemoveObject(key);
  162. }
  163. }
  164. /// <summary>
  165. /// 移出所有cache
  166. /// </summary>
  167. public virtual void RemoveAll()
  168. {
  169. lock (lockHelper)
  170. {
  171. mcs.RemoveAll();
  172. rcs?.RemoveAll();
  173. }
  174. }
  175. /// <summary>
  176. /// 批量移出cache
  177. /// </summary>
  178. /// <param name="key"></param>
  179. public virtual void RemoveObjectByRegex(string key)
  180. {
  181. if (string.IsNullOrEmpty(key))
  182. return;
  183. lock (lockHelper)
  184. {
  185. mcs.RemoveObjectByRegex(key);
  186. rcs?.RemoveObjectByRegex(key);
  187. }
  188. }
  189. /// <summary>
  190. /// 是否开启IsMemCached模型
  191. /// 请查看web.config的参数配置
  192. /// </summary>
  193. private static int CacheProvider
  194. {
  195. get
  196. {
  197. if (ConfigurationManager.AppSettings["CacheProvider"] != null)
  198. return Convert.ToInt32(ConfigurationManager.AppSettings["CacheProvider"]);
  199. return 0;
  200. }
  201. }
  202. }
  203. }