123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Globalization;
- using System.Data;
- using CP.Cache;
- using CP.Common;
- using CP.Model;
- namespace CP.Business
- {
- /// <summary>
- /// 专门的缓存清理工具类
- /// sam
- /// </summary>
- public class CacheUtils
- {
- /// <summary>
- /// 缓存
- /// </summary>
- static WMCache cache = WMCache.GetCacheService();
- /// <summary>
- /// 返回所有cache列表
- /// </summary>
- /// <returns></returns>
- public static List<string> GetCacheList()
- {
- return cache.GetKeys();
- }
- /// <summary>
- /// 清理所有Cache
- /// </summary>
- public static void RemoveAllCache()
- {
- cache.RemoveAll();
- }
- /// <summary>
- /// 根据前缀筛选获取所有Cache列表
- /// prefix对应枚举CachePrefix中的值
- /// </summary>
- /// <param name="prefix"></param>
- /// <returns></returns>
- public static List<string> GetCacheList(string prefix)
- {
- List<string> clist = cache.GetKeys();
- List<string> rlist = new List<string>();
- if (clist != null && clist.Count > 0)
- {
- foreach (string s in clist)
- {
- if (s.IndexOf(prefix, StringComparison.CurrentCultureIgnoreCase) != -1)
- rlist.Add(s);
- }
- }
- return rlist;
- }
- /// <summary>
- /// 根据key名称清除某一个cache的数据
- /// 完全匹配
- /// </summary>
- /// <param name="key"></param>
- public static void RemoveCacheByName(string key)
- {
- cache.RemoveObject(key);
- }
- /// <summary>
- /// 批量清理某一类cache数据
- /// 模糊匹配
- /// </summary>
- /// <param name="key"></param>
- public static void RemoveCacheLikeName(string key)
- {
- cache.RemoveObjectByRegex(key);
- }
- /// <summary>
- /// 清理某个彩种关于试机号的所有Cache
- /// </summary>
- /// <param name="prefix">彩种名称</param>
- public static void RemoveCacheLikeSjh(string prefix)
- {
- ///清理原始数据
- string key = "datalistall";
- if (!string.IsNullOrEmpty(prefix))
- {
- List<string> list = GetCacheList(prefix);
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
- cache.RemoveObject(list[i]);
- }
- }
- else
- {
- cache.RemoveObjectByRegex(key);
- }
-
- ///清理走势图页面数据
- key = "sjh";
- if (!string.IsNullOrEmpty(prefix))
- {
- List<string> list = GetCacheList(prefix);
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
- cache.RemoveObject(list[i]);
- }
- }
- else
- {
- cache.RemoveObjectByRegex(key);
- }
- }
- /// <summary>
- /// 清除某彩种所有缓存
- /// </summary>
- /// <param name="prefix"></param>
- public static void RemoveAllDataCache(string prefix)
- {
- var list = GetCacheList(prefix);
- foreach (var item in list)
- {
- RemoveCacheByName(item);
- }
- }
- #region 走势图/遗漏数据层面的Cache操作
- /// <summary>
- /// 清除某前缀的所有原始数据缓存
- /// prefix为空时,清除所有原始数据缓存
- /// 一级缓存
- /// </summary>
- /// <param name="prefix">可为空</param>
- public static void RemoveDataCache(string prefix = null)
- {
- string key = "datalist";
- string key2 = "kjh/data";
- if (!string.IsNullOrEmpty(prefix))
- {
- List<string> list = GetCacheList(prefix);
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1 || list[i].IndexOf(key2, StringComparison.CurrentCultureIgnoreCase) != -1)
- cache.RemoveObjectByRegex(list[i]);
- }
- }
- else
- {
- cache.RemoveObjectByRegex(key);
- }
- }
- /// <summary>
- /// 清除某前缀的所有遗漏数据缓存
- /// prefix为空时,清除所有遗漏数据缓存
- /// 二级缓存
- /// </summary>
- /// <param name="prefix">可为空</param>
- public static void RemoveMissDataCache(string prefix = null)
- {
- string key = "/yl/data/";
- if (!string.IsNullOrEmpty(prefix))
- {
- List<string> list = GetCacheList(prefix);
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
- cache.RemoveObjectByRegex(list[i]);
- }
- }
- else
- {
- cache.RemoveObjectByRegex(key);
- }
- }
- #endregion
- #region 走势图页面Cache操作
- /// <summary>
- /// 获取某前缀的所有走势图页面缓存
- /// prefix为空时,获取所有走势图页面缓存
- /// </summary>
- /// <param name="prefix"></param>
- /// <returns></returns>
- public static List<string> GetZstPageCacheList(string prefix = null)
- {
- string key = "/zst/page/";
- List<string> rlist = new List<string>();
- List<string> list = new List<string>();
- if (!string.IsNullOrEmpty(prefix))
- list = GetCacheList(prefix);
- else
- list = GetCacheList();
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
- rlist.Add(list[i]);
- }
- return rlist;
- }
- /// <summary>
- /// 清除某前缀的所有走势图页面缓存
- /// prefix为空时,清除所有走势图页面缓存
- /// 三级缓存
- /// </summary>
- /// <param name="prefix">可为空.</param>
- public static void RemoveZstPageCache(string prefix = null)
- {
- List<string> list = GetZstPageCacheList(prefix);
- for (int i = 0; i < list.Count; i++)
- {
- cache.RemoveObjectByRegex(list[i]);
- }
- }
- #endregion
- #region 遗漏页面Cache操作
- /// <summary>
- /// 获取某前缀的所有遗漏页面缓存
- /// prefix为空时,获取所有遗漏页面缓存
- /// </summary>
- /// <param name="prefix"></param>
- /// <returns></returns>
- public static List<string> GetMissPageCacheList(string prefix = null)
- {
- string key = "/yl/page/";
- List<string> rlist = new List<string>();
- List<string> list = new List<string>();
- if (!string.IsNullOrEmpty(prefix))
- list = GetCacheList(prefix);
- else
- list = GetCacheList();
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].IndexOf(key, StringComparison.CurrentCultureIgnoreCase) != -1)
- rlist.Add(list[i]);
- }
- return rlist;
- }
- /// <summary>
- /// 清除某前缀的所有遗漏页面缓存
- /// prefix为空时,清除所有遗漏页面缓存
- /// 三级缓存
- /// </summary>
- /// <param name="prefix">可为空</param>
- public static void RemoveMissPageCache(string prefix = null)
- {
- List<string> list = GetMissPageCacheList(prefix);
- for (int i = 0; i < list.Count; i++)
- {
- cache.RemoveObjectByRegex(list[i]);
- }
- }
- #endregion
- }
- }
|