WMCache.cs 5.8 KB

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