码迷,mamicode.com
首页 > Windows程序 > 详细

c# .Net 缓存 使用System.Runtime.Caching 做缓存 平滑过期,绝对过期

时间:2018-03-13 01:06:12      阅读:2144      评论:0      收藏:0      [点我收藏+]

标签:param   off   res   stat   ova   指定   caching   items   实例   

  1 public class CacheHeloer
  2 {
  3 
  4     /// <summary>
  5     /// 默认缓存
  6     /// </summary>
  7     private static CacheHeloer Default { get { return new CacheHeloer(); } }
  8 
  9     /// <summary>
 10     /// 缓存初始化
 11     /// </summary>
 12     private MemoryCache cache = MemoryCache.Default;
 13 
 14     /// <summary>
 15     /// 16     /// </summary>
 17     private object locker = new object();
 18 
 19     /// <summary>
 20     /// 构造器
 21     /// </summary>
 22     private CacheHeloer()
 23     {
 24         //CacheItemPolicy policy = new CacheItemPolicy();  //创建缓存项策略
 25         ////过期时间设置,以下两种只能设置一种
 26         //policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(5)); //设定某个时间过后将逐出缓存
 27         //policy.SlidingExpiration = new TimeSpan(0, 0, 10);    //设定某个时间段内未被访问将逐出缓存
 28         ////逐出通知,以下两种只能设置一种
 29         //policy.UpdateCallback = arguments => { Console.WriteLine("即将逐出缓存" + arguments.Key); };
 30     }
 31 
 32     /// <summary>
 33     /// 从缓存中获取对象
 34     /// </summary>
 35     /// <typeparam name="T">对象类型</typeparam>
 36     /// <param name="key"></param>
 37     /// <returns>缓存对象</returns>
 38     public static object Get(string key)
 39     {
 40         return Default.GetFromCache(key);
 41     }
 42 
 43     /// <summary>
 44     /// 从缓存中获取对象
 45     /// </summary>
 46     /// <typeparam name="T">对象类型</typeparam>
 47     /// <param name="key"></param>
 48     /// <returns>缓存对象</returns>
 49     private object GetFromCache(string key)
 50     {
 51         lock (locker)
 52         {
 53             if (cache.Contains(key))
 54             {
 55                 return cache[key];
 56             }
 57             return null;
 58         }
 59     }
 60 
 61     /// <summary>
 62     /// 设置缓存指定时间未访问过期
 63     /// </summary>
 64     /// <typeparam name="T">对象</typeparam>
 65     /// <param name="key"></param>
 66     /// <param name="value">数据对象</param>
 67     /// <param name="expire">过期时间</param>
 68     public static bool Set(string key, Object value, TimeSpan expiresIn)
 69     {
 70         var policy = new CacheItemPolicy()
 71         {
 72             SlidingExpiration = expiresIn
 73         };
 74         return Default.SetToCache(key, value, policy);
 75     }
 76     /// <summary>
 77     /// 设置缓存绝对时间过期
 78     /// </summary>
 79     /// <typeparam name="T"></typeparam>
 80     /// <param name="key"></param>
 81     /// <param name="value"></param>
 82     /// <param name="expiresIn"></param>
 83     /// <returns></returns>
 84     public static bool Set(string key, Object value, DateTimeOffset expiresIn)
 85     {
 86         var policy = new CacheItemPolicy()
 87         {
 88             AbsoluteExpiration = expiresIn
 89         };
 90         return Default.SetToCache(key, value, policy);
 91     }
 92 
 93     /// <summary>
 94     /// 添加到缓存
 95     /// </summary>
 96     /// <typeparam name="T">缓存对象类型</typeparam>
 97     /// <param name="key"></param>
 98     /// <param name="value"></param>
 99     /// <returns>结果状态</returns>
100     public static bool Set(string key, object value)
101     {
102         CacheItemPolicy policy = new CacheItemPolicy()
103         {
104             Priority = CacheItemPriority.NotRemovable,
105         };
106         return Default.SetToCache(key, value, policy);
107     }
108 
109     /// <summary>
110     /// 数据对象装箱缓存
111     /// </summary>
112     /// <typeparam name="T">对象</typeparam>
113     /// <param name="key"></param>
114     /// <param name="value">数据对象</param>
115     /// <param name="expire">过期时间</param>
116     private bool SetToCache(string key, object value, CacheItemPolicy policy)
117     {
118         lock (locker)
119         {
120             cache.Set(key, value, policy);
121             return true;
122         }
123     }
124 
125     /// <summary>
126     /// 获取键的集合
127     /// </summary>
128     /// <returns>键的集合</returns>
129     public static ICollection<string> Keys()
130     {
131         return Default.GetCacheKeys();
132     }
133 
134     /// <summary>
135     /// 获取键的集合
136     /// </summary>
137     /// <returns>键的集合</returns>
138     private ICollection<string> GetCacheKeys()
139     {
140         lock (locker)
141         {
142             IEnumerable<KeyValuePair<string, object>> items = cache.AsEnumerable();
143             return items.Select(m => m.Key).ToList();
144         }
145     }
146 
147     /// <summary>
148     /// 判断缓存中是否有此对象
149     /// </summary>
150     /// <param name="key"></param>
151     /// <returns>是否存在</returns>
152     public static bool Contain(string key)
153     {
154         return Default.ContainKey(key);
155     }
156 
157     /// <summary>
158     /// 判断缓存中是否有此对象
159     /// </summary>
160     /// <param name="key"></param>
161     /// <returns>是否存在</returns>
162     private bool ContainKey(string key)
163     {
164         lock (locker)
165         {
166             return cache.Contains(key);
167         }
168     }
169 
170     /// <summary>
171     /// 数据对象从缓存对象中移除
172     /// </summary>
173     /// <param name="key"></param>
174     public static bool Remove(string key)
175     {
176         return Default.RemoveFromCache(key);
177     }
178 
179     /// <summary>
180     /// 数据对象从缓存对象中移除
181     /// </summary>
182     /// <param name="key"></param>
183     private bool RemoveFromCache(string key)
184     {
185         lock (locker)
186         {
187             if (cache.Contains(key))
188             {
189                 cache.Remove(key);
190                 return true;
191             }
192             return false;
193         }
194     }
195 
196     /// <summary>
197     /// 清除实例
198     /// </summary>
199     public static void Clear()
200     {
201         Default.ClearCache();
202     }
203 
204     /// <summary>
205     /// 清除实例
206     /// </summary>
207     private void ClearCache()
208     {
209         lock (locker)
210         {
211             cache.ToList().ForEach(m => cache.Remove(m.Key));
212         }
213     }
214 
215     /// <summary>
216     /// 获取缓存对象集合
217     /// </summary>
218     /// <typeparam name="T">缓存对象类型</typeparam>
219     /// <returns>缓存对象集合</returns>
220     public static ICollection<T> Values<T>()
221     {
222         return Default.GetValues<T>();
223     }
224 
225     /// <summary>
226     /// 获取缓存对象集合
227     /// </summary>
228     /// <typeparam name="T">缓存对象类型</typeparam>
229     /// <returns>缓存对象集合</returns>
230     private ICollection<T> GetValues<T>()
231     {
232         lock (locker)
233         {
234             IEnumerable<KeyValuePair<string, object>> items = cache.AsEnumerable();
235             return items.Select(m => (T)m.Value).ToList();
236         }
237     }
238 
239     /// <summary>
240     /// 获取缓存尺寸
241     /// </summary>
242     /// <returns>缓存尺寸</returns>
243     public static long Size()
244     {
245         return Default.GetCacheSize();
246     }
247 
248     /// <summary>
249     /// 获取缓存尺寸
250     /// </summary>
251     /// <returns>缓存尺寸</returns>
252     private long GetCacheSize()
253     {
254         lock (locker)
255         {
256             return cache.GetCount();
257         }
258     }
259 }

 

c# .Net 缓存 使用System.Runtime.Caching 做缓存 平滑过期,绝对过期

标签:param   off   res   stat   ova   指定   caching   items   实例   

原文地址:https://www.cnblogs.com/aaaaq/p/8552320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!