HttpRuntime.Cache .Net自帶的緩存類

.Net自帶的緩存有兩個,一個是Asp.Net的緩存 HttpContext.Cache,一個是.Net應用程序級別的緩存,HttpRuntime.Cache。緩存

MSDN上有解釋說:spa

HttpContext.Current.Cache:爲當前 HTTP 請求獲取Cache對象。
HttpRuntime.Cache:獲取當前應用程序的Cache。 code

經過源碼查看能夠知悉,HttpContext.Current.Cache調用的居然也是HttpRuntime.Cache並且HttpContext只能在Web下調用,而HttpRuntimeCache能夠在任何應用程序集中使用,所以,這裏我直接封裝HttpRuntimeCache做爲緩存類來使用。對象

代碼以下:blog

  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 }
相關文章
相關標籤/搜索