CacheHelper

private List<MENU> _Menu; 
public List<MENU> Menu
{
    get
    {
        if (_Menu == null)
        {
            if (!CacheHelper.IsSet("menus"))
            { 
                CacheHelper.Set("menus", BllSession.IMENU_BLL.GetListByWhere(s => s.IsDel == 0));
            }
            var menus = Menus.Select(s => s.MENUID);
            _Menu = CacheHelper.Get<List<MENU>>("menus").Where(s => menus.Contains(s.MENUID)).ToList();
        }
        return _Menu;
    } 
}
 1 public class CacheHelper
 2 {
 3     static System.Web.Caching.Cache Cache = HttpRuntime.Cache;
 4 
 5     public static void Set(string key, object data)
 6     {
 7         Cache.Insert(key, data);
 8     }
 9 
10     public static void Set(string key, object data, DateTime absoluteExpiration)
11     {
12         Cache.Insert(key, data, null, absoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);
13     }
14 
15     public static void Set(string key, object data, TimeSpan slidingExpiration)
16     {
17         Cache.Insert(key, data, null,System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpiration);
18     }
19      
20 
21     public static void Set(string key, object data, string fileName)
22     {
23         Cache.Insert(key, data, new System.Web.Caching.CacheDependency(fileName)); 
24     }
25      
26     public static void Set(string key, object data,System.Web.Caching.CacheItemRemovedCallback callback)
27     {
28         Cache.Insert(key, data, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, callback); 
29     }
30 
31     public static void Set(string key, object data, System.Web.Caching.CacheItemUpdateCallback callback)
32     {
33         Cache.Insert(key, data, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, callback);
34     }
35     
36     public static object Get(string Key)
37     {
38         return Cache[Key];
39     }
40 
41     public static T Get<T>(string key)
42     {
43         return (T)Cache[key];
44     }
45 
46     public static bool IsSet(string key)
47     {
48         return Cache[key] != null;
49     }
50 
51     public static void Remove(string Key)
52     {
53         if (Cache[Key] != null)
54         {
55             Cache.Remove(Key);
56         }
57     }
58 
59     public static void RemoveByPattern(string pattern)
60     {
61         IDictionaryEnumerator enumerator = Cache.GetEnumerator();
62         Regex rgx = new Regex(pattern, (RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase)));
63         while (enumerator.MoveNext())
64         {
65             if (rgx.IsMatch(enumerator.Key.ToString()))
66             {
67                 Cache.Remove(enumerator.Key.ToString());
68             }
69         }
70     }
71 
72     public static void Clear()
73     {
74         IDictionaryEnumerator enumerator = Cache.GetEnumerator();
75         while (enumerator.MoveNext())
76         {
77             Cache.Remove(enumerator.Key.ToString());
78         }
79     }
80 }
81  
CacheHelper
原文地址:https://www.cnblogs.com/valeb/p/7634528.html