C# Cache緩存讀取設置

先創建一個CacheHelper.cs類,代碼如下

 

[csharp] view plain copy
  1. using System;  
  2. using System.Web;  
  3. using System.Collections;  
  4. using System.Web.Caching;  
  5.   
  6. public class CacheHelper  
  7. {  
  8.     /// <summary>  
  9.     /// 獲取數據緩存  
  10.     /// </summary>  
  11.     /// <param name="cacheKey">鍵</param>  
  12.     public static object GetCache(string cacheKey)  
  13.     {  
  14.         var objCache = HttpRuntime.Cache.Get(cacheKey);  
  15.         return objCache;  
  16.     }  
  17.     /// <summary>  
  18.     /// 設置數據緩存  
  19.     /// </summary>  
  20.     public static void SetCache(string cacheKey, object objObject)  
  21.     {  
  22.         var objCache = HttpRuntime.Cache;  
  23.         objCache.Insert(cacheKey, objObject);  
  24.     }  
  25.     /// <summary>  
  26.     /// 設置數據緩存  
  27.     /// </summary>  
  28.     public static void SetCache(string cacheKey, object objObject, int timeout = 7200)  
  29.     {  
  30.         try  
  31.         {  
  32.             if (objObject == null) return;  
  33.             var objCache = HttpRuntime.Cache;  
  34.             //相對過期  
  35.             //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);  
  36.             //絕對過期時間  
  37.             objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);  
  38.         }  
  39.         catch (Exception)  
  40.         {  
  41.             //throw;  
  42.         }  
  43.     }  
  44.     /// <summary>  
  45.     /// 移除指定數據緩存  
  46.     /// </summary>  
  47.     public static void RemoveAllCache(string cacheKey)  
  48.     {  
  49.         var cache = HttpRuntime.Cache;  
  50.         cache.Remove(cacheKey);  
  51.     }  
  52.     /// <summary>  
  53.     /// 移除全部緩存  
  54.     /// </summary>  
  55.     public static void RemoveAllCache()  
  56.     {  
  57.         var cache = HttpRuntime.Cache;  
  58.         var cacheEnum = cache.GetEnumerator();  
  59.         while (cacheEnum.MoveNext())  
  60.         {  
  61.             cache.Remove(cacheEnum.Key.ToString());  
  62.         }  
  63.     }  
  64. }  


引用也貼在上面了,就這麼幾個。

然後是調用:

 

 

[csharp] view plain copy
  1. public IEnumerable<CompanyModel> FindCompanys()  
  2.         {  
  3.             var cache = CacheHelper.GetCache("commonData_Company");//先讀取  
  4.             if (cache == null)//如果沒有該緩存  
  5.             {  
  6.                 var queryCompany = _base.CompanyModel();//從數據庫取出  
  7.                 var enumerable = queryCompany.ToList();  
  8.                 CacheHelper.SetCache("commonData_Company", enumerable);//添加緩存  
  9.                 return enumerable;  
  10.             }  
  11.             var result = (List<CompanyModel>)cache;//有就直接返回該緩存  
  12.             return result;  
  13.         }  


測試結果也貼上來看看好了:


首次加載進來是爲null,然後讀取數據庫,添加進緩存,當前返回前臺的是從數據庫中取出的數據。


刷新頁面,發現緩存中已經有了讀出的30條數據,


然後接下來走,返回緩存中的數據:

大致這些了。

End

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章