.NET 缓存设计的使用说明

2019-05-20 12:05:27刘景俊


         /// <summary>
         /// 单件模式
         /// </summary>
         static BaseCache()
         {
             HttpContext context = HttpContext.Current;
             if (context != null)
             {
                 _cache = context.Cache;
             }
             else
             {
                 _cache = HttpRuntime.Cache;
             }
         }

         /// <summary>
         /// 一次性清除所有缓存
         /// </summary>
         public static void Clear()
         {
             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
             ArrayList al = new ArrayList();
             while (CacheEnum.MoveNext()) //逐个清除
             {
                 al.Add(CacheEnum.Key);
             }

             foreach (string key in al)
             {
                 _cache.Remove(key);
             }

         }

 

         public static void RemoveByPattern(string pattern)
         {
             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
             Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);