CacheUtils.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Globalization;
  7. using System.Data;
  8. using CP.Cache;
  9. using CP.Common;
  10. using CP.Model;
  11. namespace CP.Business
  12. {
  13. /// <summary>
  14. /// 专门的缓存清理工具类
  15. /// sam
  16. /// </summary>
  17. public class CacheUtils
  18. {
  19. /// <summary>
  20. /// 缓存
  21. /// </summary>
  22. static WMCache cache = WMCache.GetCacheService();
  23. /// <summary>
  24. /// 返回所有cache列表
  25. /// </summary>
  26. /// <returns></returns>
  27. public static List<string> GetCacheList()
  28. {
  29. return cache.GetKeys();
  30. }
  31. /// <summary>
  32. /// 清理所有Cache
  33. /// </summary>
  34. public static void RemoveAllCache()
  35. {
  36. cache.RemoveAll();
  37. }
  38. /// <summary>
  39. /// 根据前缀筛选获取所有Cache列表
  40. /// prefix对应枚举CachePrefix中的值
  41. /// </summary>
  42. /// <param name="prefix"></param>
  43. /// <returns></returns>
  44. public static List<string> GetCacheList(string prefix)
  45. {
  46. List<string> clist = cache.GetKeys();
  47. List<string> rlist = new List<string>();
  48. if (clist != null && clist.Count > 0)
  49. {
  50. foreach (string s in clist)
  51. {
  52. if (s.IndexOf(prefix, StringComparison.CurrentCultureIgnoreCase) != -1)
  53. rlist.Add(s);
  54. }
  55. }
  56. return rlist;
  57. }
  58. /// <summary>
  59. /// 根据key名称清除某一个cache的数据
  60. /// 完全匹配
  61. /// </summary>
  62. /// <param name="key"></param>
  63. public static void RemoveCacheByName(string key)
  64. {
  65. cache.RemoveObject(key);
  66. }
  67. /// <summary>
  68. /// 批量清理某一类cache数据
  69. /// 模糊匹配
  70. /// </summary>
  71. /// <param name="key"></param>
  72. public static void RemoveCacheLikeName(string key)
  73. {
  74. cache.RemoveObjectByRegex(key);
  75. }
  76. /// <summary>
  77. /// 清理某个彩种关于试机号的所有Cache
  78. /// </summary>
  79. /// <param name="prefix">彩种名称</param>
  80. public static void RemoveCacheLikeSjh(string prefix)
  81. {
  82. ///清理原始数据
  83. string key = "datalistall";
  84. if (!string.IsNullOrEmpty(prefix))
  85. {
  86. List<string> list = GetCacheList(prefix);
  87. for (int i = 0; i < list.Count; i++)
  88. {
  89. if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
  90. cache.RemoveObject(list[i]);
  91. }
  92. }
  93. else
  94. {
  95. cache.RemoveObjectByRegex(key);
  96. }
  97. ///清理走势图页面数据
  98. key = "sjh";
  99. if (!string.IsNullOrEmpty(prefix))
  100. {
  101. List<string> list = GetCacheList(prefix);
  102. for (int i = 0; i < list.Count; i++)
  103. {
  104. if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
  105. cache.RemoveObject(list[i]);
  106. }
  107. }
  108. else
  109. {
  110. cache.RemoveObjectByRegex(key);
  111. }
  112. }
  113. /// <summary>
  114. /// 清除某彩种所有缓存
  115. /// </summary>
  116. /// <param name="prefix"></param>
  117. public static void RemoveAllDataCache(string prefix)
  118. {
  119. var list = GetCacheList(prefix);
  120. foreach (var item in list)
  121. {
  122. RemoveCacheByName(item);
  123. }
  124. }
  125. #region 走势图/遗漏数据层面的Cache操作
  126. /// <summary>
  127. /// 清除某前缀的所有原始数据缓存
  128. /// prefix为空时,清除所有原始数据缓存
  129. /// 一级缓存
  130. /// </summary>
  131. /// <param name="prefix">可为空</param>
  132. public static void RemoveDataCache(string prefix = null)
  133. {
  134. string key = "datalist";
  135. string key2 = "kjh/data";
  136. if (!string.IsNullOrEmpty(prefix))
  137. {
  138. List<string> list = GetCacheList(prefix);
  139. for (int i = 0; i < list.Count; i++)
  140. {
  141. if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1 || list[i].IndexOf(key2, StringComparison.CurrentCultureIgnoreCase) != -1)
  142. cache.RemoveObjectByRegex(list[i]);
  143. }
  144. }
  145. else
  146. {
  147. cache.RemoveObjectByRegex(key);
  148. }
  149. }
  150. /// <summary>
  151. /// 清除某前缀的所有遗漏数据缓存
  152. /// prefix为空时,清除所有遗漏数据缓存
  153. /// 二级缓存
  154. /// </summary>
  155. /// <param name="prefix">可为空</param>
  156. public static void RemoveMissDataCache(string prefix = null)
  157. {
  158. string key = "/yl/data/";
  159. if (!string.IsNullOrEmpty(prefix))
  160. {
  161. List<string> list = GetCacheList(prefix);
  162. for (int i = 0; i < list.Count; i++)
  163. {
  164. if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
  165. cache.RemoveObjectByRegex(list[i]);
  166. }
  167. }
  168. else
  169. {
  170. cache.RemoveObjectByRegex(key);
  171. }
  172. }
  173. #endregion
  174. #region 走势图页面Cache操作
  175. /// <summary>
  176. /// 获取某前缀的所有走势图页面缓存
  177. /// prefix为空时,获取所有走势图页面缓存
  178. /// </summary>
  179. /// <param name="prefix"></param>
  180. /// <returns></returns>
  181. public static List<string> GetZstPageCacheList(string prefix = null)
  182. {
  183. string key = "/zst/page/";
  184. List<string> rlist = new List<string>();
  185. List<string> list = new List<string>();
  186. if (!string.IsNullOrEmpty(prefix))
  187. list = GetCacheList(prefix);
  188. else
  189. list = GetCacheList();
  190. for (int i = 0; i < list.Count; i++)
  191. {
  192. if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
  193. rlist.Add(list[i]);
  194. }
  195. return rlist;
  196. }
  197. /// <summary>
  198. /// 清除某前缀的所有走势图页面缓存
  199. /// prefix为空时,清除所有走势图页面缓存
  200. /// 三级缓存
  201. /// </summary>
  202. /// <param name="prefix">可为空.</param>
  203. public static void RemoveZstPageCache(string prefix = null)
  204. {
  205. List<string> list = GetZstPageCacheList(prefix);
  206. for (int i = 0; i < list.Count; i++)
  207. {
  208. cache.RemoveObjectByRegex(list[i]);
  209. }
  210. }
  211. #endregion
  212. #region 遗漏页面Cache操作
  213. /// <summary>
  214. /// 获取某前缀的所有遗漏页面缓存
  215. /// prefix为空时,获取所有遗漏页面缓存
  216. /// </summary>
  217. /// <param name="prefix"></param>
  218. /// <returns></returns>
  219. public static List<string> GetMissPageCacheList(string prefix = null)
  220. {
  221. string key = "/yl/page/";
  222. List<string> rlist = new List<string>();
  223. List<string> list = new List<string>();
  224. if (!string.IsNullOrEmpty(prefix))
  225. list = GetCacheList(prefix);
  226. else
  227. list = GetCacheList();
  228. for (int i = 0; i < list.Count; i++)
  229. {
  230. if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
  231. rlist.Add(list[i]);
  232. }
  233. return rlist;
  234. }
  235. /// <summary>
  236. /// 清除某前缀的所有遗漏页面缓存
  237. /// prefix为空时,清除所有遗漏页面缓存
  238. /// 三级缓存
  239. /// </summary>
  240. /// <param name="prefix">可为空</param>
  241. public static void RemoveMissPageCache(string prefix = null)
  242. {
  243. List<string> list = GetMissPageCacheList(prefix);
  244. for (int i = 0; i < list.Count; i++)
  245. {
  246. cache.RemoveObjectByRegex(list[i]);
  247. }
  248. }
  249. #endregion
  250. }
  251. }