using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Web; using System.Web.Cac ...
歡迎各位大神提建議,目前的需求,這個工具類已經夠用了。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Web; using System.Web.Caching; namespace Tools { /// <summary> /// 緩存輔助類 /// </summary> public static class CacheHelper { /// <summary> /// 緩存 /// </summary> private static Cache cache = HttpRuntime.Cache; /// <summary> /// 根據鍵獲取緩存數據 /// </summary> /// <param name="cacheKey">緩存的鍵</param> /// <returns></returns> private static object GetCache(string cacheKey) { return cache.Get(cacheKey); } /// <summary> /// 設置緩存 /// </summary> /// <param name="cacheKey">緩存的鍵</param> /// <param name="objValue">緩存的值</param> private static void SetCache(string cacheKey, object objValue) { cache.Insert(cacheKey, objValue); } /// <summary> /// 設置緩存 /// </summary> /// <param name="cacheKey">緩存的鍵</param> /// <param name="objValue">緩存的值</param> /// <param name="slidingExpiration">滑動過期時間</param> private static void SetCache(string cacheKey, object objValue, TimeSpan slidingExpiration) { cache.Insert(cacheKey, objValue, null, Cache.NoAbsoluteExpiration, slidingExpiration); } /// <summary> /// 設置緩存 /// </summary> /// <param name="cacheKey">緩存的鍵</param> /// <param name="objValue">緩存的值</param> /// <param name="absoluteExpiration">絕對過期時間</param> /// <param name="slidingExpiration">滑動過期時間</param> private static void SetCache(string cacheKey, object objValue, DateTime absoluteExpiration) { cache.Insert(cacheKey, objValue, null, absoluteExpiration, Cache.NoSlidingExpiration); } /// <summary> /// 移除指定的緩存 /// </summary> /// <param name="cacheKey">緩存的鍵</param> public static void Remove(string cacheKey) { cache.Remove(cacheKey); } /// <summary> /// 移除全部緩存 /// </summary> public static void Remove() { IDictionaryEnumerator CacheEnum = cache.GetEnumerator(); while (CacheEnum.MoveNext()) { cache.Remove(CacheEnum.Key.ToString()); } } /// <summary> /// 從緩存中獲取數據。緩存中不存在的時候,從回調方法getDate中獲取,並設置進緩存。 /// </summary> /// <typeparam name="T">緩存的數據類型</typeparam> /// <param name="cacheKey">緩存的鍵</param> /// <param name="getData">回調方法</param> /// <returns>緩存中的數據</returns> public static T Get<T>(string cacheKey, Func<T> getData) { var data = GetCache(cacheKey); if (data == null) { data = getData(); SetCache(cacheKey, data); } return (T)data; } /// <summary> /// 從緩存中獲取數據。緩存中不存在的時候,從回調方法getDate中獲取,並設置進緩存。 /// </summary> /// <typeparam name="T">緩存的數據類型</typeparam> /// <param name="cacheKey">緩存的鍵</param> /// <param name="slidingExpiration">滑動過期時間</param> /// <param name="getData">回調方法</param> /// <returns>緩存中的數據</returns> public static T Get<T>(string cacheKey, TimeSpan slidingExpiration, Func<T> getData) { var data = GetCache(cacheKey); if (data == null) { data = getData(); SetCache(cacheKey, data, slidingExpiration); } return (T)data; } /// <summary> /// 從緩存中獲取數據。緩存中不存在的時候,從回調方法getDate中獲取,並設置進緩存。 /// </summary> /// <typeparam name="T">緩存的數據類型</typeparam> /// <param name="cacheKey">緩存的鍵</param> /// <param name="absoluteExpiration">絕對過期時間</param> /// <param name="getData">回調方法</param> /// <returns>緩存中的數據</returns> public static T Get<T>(string cacheKey, DateTime absoluteExpiration, Func<T> getData) { var data = GetCache(cacheKey); if (data == null) { data = getData(); SetCache(cacheKey, data, absoluteExpiration); } return (T)data; } } }View Code