.net一般缓存帮助类

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Runtime.Caching;
  5 using System.Text;
  6 
  7 namespace Components.Helper
  8 {
  9 /// 
 10 /// 一般缓存类
 11 /// 
 12 public class CacheHelper
 13 {
 14 static CacheItemPolicy cacheItemPolicy = new CacheItemPolicy()
 15 {
 16 RemovedCallback = new CacheEntryRemovedCallback(CacheEntryRemoved),
 17 SlidingExpiration = new TimeSpan(0, 20, 0)
 18 };
 19 
 20 static void CacheEntryRemoved(CacheEntryRemovedArguments arg)
 21 {
 22 //Entities db = new Entities();
 23 //var list = db.ESet.ToList();
 24 // Cache.AddCache(list);
 25 }
 26 
 27 
 28 /// 新增缓存
 29 /// 缓存Key
 30 /// 缓存数据
 31 public static void AddCache(string key, object data)
 32 {
 33 if (MemoryCache.Default.Contains(key))
 34 {
 35 MemoryCache.Default.Remove(key);
 36 }
 37 MemoryCache.Default.Add(key, data, cacheItemPolicy);
 38 
 39 }
 40 
 41  
 42 
 43 /// 
 44 /// 新增缓存
 45 /// 
 46 /// 缓存的唯一标识key
 47 /// 缓存的数据
 48 /// 过期时间(分钟)
 49 public static void AddCache(string key, object data, int outMinutes)
 50 {
 51 
 52 if (MemoryCache.Default.Contains(key))
 53 {
 54 MemoryCache.Default.Remove(key);
 55 }
 56 CacheItemPolicy _cacheItemPolicy = new CacheItemPolicy()
 57 {
 58 RemovedCallback = new CacheEntryRemovedCallback(CacheEntryRemoved),
 59 SlidingExpiration = new TimeSpan(0, outMinutes, 0)
 60 };
 61 MemoryCache.Default.Add(key, data, _cacheItemPolicy);
 62 
 63 }
 64 
 65 /// 
 66 /// 根据key获取单个缓存
 67 /// 
 68 /// 
 69 /// 
 70 public static object GetCache(string key)
 71 {
 72 
 73 if (MemoryCache.Default.Contains(key))
 74 {
 75 return (object)MemoryCache.Default[key];
 76 }
 77 return null;
 78 
 79 
 80 }
 81 
 82 
 83 /// 移除缓存
 84 /// 缓存Key
 85 public static void RemoveCache(string key)
 86 {
 87 if (MemoryCache.Default.Contains(key))
 88 {
 89 MemoryCache.Default.Remove(key);
 90 }
 91 }
 92 
 93 /// 
 94 /// 移除缓存,针对空间
 95 /// 
 96 /// 
 97 public static void RemoveRegionCache(string regionName)
 98 {
 99 var cacheList = MemoryCache.Default.Where(q => q.Key.StartsWith(regionName + ":")).ToList();
100 foreach (var item in cacheList)
101 {
102 MemoryCache.Default.Remove(item.Key);
103 }
104 }
105 
106 /// 
107 /// 新增分布式缓存
108 /// 
109 /// 缓存Key
110 /// 缓存数据
111 /// 是否过期 默认过期事件20分钟 webconfig中配置
112 public static void AddMemcachedCache(string key, object data, bool p_IsOutDate)
113 {
114 if (MemcachHelper.IsEnableScatteredCache)
115 {
116 if (MemcachHelper.KeyExists(key))
117 {
118 MemcachHelper.Delete(key);
119 }
120 if (p_IsOutDate)
121 {
122 //初始化过期时间
123 DateTime m_expiryTime = DateTime.Now.AddMilliseconds(MemcachHelper.DefaultCacheTime);
124 MemcachHelper.Add(key, data, m_expiryTime);
125 }
126 else
127 {
128 MemcachHelper.Add(key, data);
129 }
130 }
131 }
132 
133 /// 
134 /// 根据key获取单个分布式缓存
135 /// 
136 /// 
137 /// 
138 public static object GetMemcachedCache(string key)
139 {
140 if (MemcachHelper.IsEnableScatteredCache)
141 {
142 if (MemcachHelper.KeyExists(key))
143 {
144 return MemcachHelper.Get(key);
145 }
146 }
147 return null;
148 }
149 
150 /// 
151 /// 根据key删除单个分布式缓存
152 /// 
153 /// 
154 /// 
155 public static void RemoveMemcachedCache(string key)
156 {
157 if (MemcachHelper.IsEnableScatteredCache)
158 {
159 if (MemcachHelper.KeyExists(key))
160 {
161 MemcachHelper.Delete(key);
162 }
163 }
164 }
165 }
166 }

 

转载于:https://www.cnblogs.com/HuberyHu/p/5318739.html

你可能感兴趣的:(runtime)