1. 程式人生 > >HttpRuntime.Cache .Net自帶的快取類

HttpRuntime.Cache .Net自帶的快取類

複製程式碼
  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 }
複製程式碼