HttpRuntime.Cache .Net自带的缓存类

.Net自带的缓存有两个,一个是Asp.Net的缓存 HttpContext.Cache,一个是.Net应用程序级别的缓存,HttpRuntime.Cache。

MSDN上有解释说:

HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。
HttpRuntime.Cache:获取当前应用程序的Cache。 

通过源码查看可以知悉,HttpContext.Current.Cache调用的竟然也是HttpRuntime.Cache而且HttpContext只能在Web下调用,而HttpRuntimeCache可以在任何应用程序集中使用,因此,这里我直接封装HttpRuntimeCache作为缓存类来使用。

代码如下:

  1 using System;
  2 using System.Collections;
  3 using System.Web;
  4 using System.Web.Caching;
  5 /**
  6 * author:qixiao
  7 * create2017-6-6 11:54:07
  8 * */
  9 namespace QX_Frame.Helper_DG
 10 {
 11     public class HttpRuntimeCache_Helper_DG
 12     {
 13         /// <summary>
 14         /// Cache_Get
 15         /// </summary>
 16         /// <param name="cacheKey">cacheKey</param>
 17         /// <returns></returns>
 18         public static object Cache_Get(string cacheKey)
 19         {
 20             return HttpRuntime.Cache[cacheKey];
 21         }
 22 
 23         #region Cache Add
 24 
 25         /// <summary>
 26         /// Cache_Add
 27         /// </summary>
 28         /// <param name="cacheKey">key</param>
 29         /// <param name="cacheValue">object value</param>
 30         /// <param name="keepMinutes"></param>
 31         /// <param name="dependencies">缓存的依赖项,也就是此项的更改意味着缓存内容已经过期。如果没有依赖项,可将此值设置为NULL。</param>
 32         /// <param name="cacheItemRemovedCallback">表示缓存删除数据对象时调用的事件,一般用做通知程序。</param>
 33         /// <returns></returns>
 34         public static Boolean Cache_Add(string cacheKey, object cacheValue, int keepMinutes = 10, CacheDependency dependencies = null, CacheItemRemovedCallback cacheItemRemovedCallback = null)
 35         {
 36             HttpRuntime.Cache.Insert(cacheKey, cacheValue, dependencies, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(keepMinutes), CacheItemPriority.NotRemovable, cacheItemRemovedCallback);
 37             return true;
 38         }
 39 
 40         /// <summary>
 41         /// Cache_Add
 42         /// </summary>
 43         /// <param name="cacheKey">key</param>
 44         /// <param name="cacheValue">object value</param>
 45         /// <param name="keepMinutes"></param>
 46         /// <param name="dependencies">缓存的依赖项,也就是此项的更改意味着缓存内容已经过期。如果没有依赖项,可将此值设置为NULL。</param>
 47         /// <param name="cacheItemRemovedCallback">表示缓存删除数据对象时调用的事件,一般用做通知程序。</param>
 48         /// <returns></returns>
 49         public static Boolean Cache_Add(string cacheKey, object cacheValue, DateTime expireTime, CacheDependency dependencies = null, CacheItemRemovedCallback cacheItemRemovedCallback = null)
 50         {
 51             HttpRuntime.Cache.Insert(cacheKey, cacheValue, dependencies, expireTime, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, cacheItemRemovedCallback);
 52             return true;
 53         }
 54 
 55         /// <summary>
 56         /// Cache_Add
 57         /// </summary>
 58         /// <param name="cacheKey">key</param>
 59         /// <param name="cacheValue">object value</param>
 60         /// <param name="dependencies">缓存的依赖项,也就是此项的更改意味着缓存内容已经过期。如果没有依赖项,可将此值设置为NULL。</param>
 61         /// <param name="absoluteExpiration">如果设置slidingExpiration,则该项必须设置为DateTime.MaxValue。是日期型数据,表示缓存过期的时间,.NET 2.0提供的缓存在过期后是可以使用的,能使用多长时间,就看这个参数的设置。</param>
 62         /// <param name="slidingExpiration">如果设置absoluteExpiration,则该项必须设置为TimeSpan.Zero。表示一段时间间隔,表示缓存参数将在多长时间以后被删除,此参数与absoluteExpiration参数相关联。</param>
 63         /// <param name="cacheItemPriority">表示撤销缓存的优先值,此参数的值取自枚举变量“CacheItemPriority”,优先级低的数据项将先被删除。此参数主要用在缓存退出对象时。</param>
 64         /// <param name="cacheItemRemovedCallback">表示缓存删除数据对象时调用的事件,一般用做通知程序。</param>
 65         //public static Boolean Cache_Add(string cacheKey, object cacheValue, CacheDependency dependencies = null, DateTime absoluteExpiration = default(DateTime), TimeSpan slidingExpiration = default(TimeSpan), CacheItemPriority cacheItemPriority = CacheItemPriority.NotRemovable, CacheItemRemovedCallback cacheItemRemovedCallback = null)
 66         //{
 67         //    DateTime absoluteExpirationTime = default(DateTime);
 68         //    if (!DateTime.TryParse(absoluteExpiration.ToString(), out absoluteExpirationTime) || absoluteExpiration.Equals(default(DateTime)))
 69         //        absoluteExpirationTime = DateTime.MaxValue;
 70         //    else
 71         //        slidingExpiration = TimeSpan.Zero;
 72 
 73         //    TimeSpan slidingExpirationTime = default(TimeSpan);
 74         //    if (!TimeSpan.TryParse(slidingExpiration.ToString(), out slidingExpirationTime) || slidingExpiration.Equals(default(TimeSpan)))
 75         //        slidingExpirationTime = TimeSpan.Zero;
 76 
 77         //    HttpRuntime.Cache.Insert(cacheKey, cacheValue, dependencies, absoluteExpirationTime, slidingExpirationTime, cacheItemPriority, cacheItemRemovedCallback);
 78         //    return true;
 79         //}
 80 
 81         #endregion
 82 
 83         /// <summary>
 84         /// Cache_Delete
 85         /// </summary>
 86         /// <param name="cacheKey">cacheKey</param>
 87         public static Boolean Cache_Delete(string cacheKey)
 88         {
 89             HttpRuntime.Cache.Remove(cacheKey);
 90             return true;
 91         }
 92 
 93         /// <summary>
 94         /// Cache_DeleteAll
 95         /// </summary>
 96         public static Boolean Cache_DeleteAll()
 97         {
 98             Cache _cache = HttpRuntime.Cache;
 99             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
100             while (CacheEnum.MoveNext())
101             {
102                 _cache.Remove(CacheEnum.Key.ToString());
103             }
104             return true;
105         }
106 
107         /// <summary>
108         /// Cache Count
109         /// </summary>
110         public static int CacheCount
111         {
112             get { return HttpRuntime.Cache.Count; }
113         }
114     }
115 }

 

posted @ 2017-06-14 10:25  7tiny  阅读(903)  评论(2编辑  收藏  举报