C# Cache緩存讀取的設(shè)置方法
先創(chuàng)建一個(gè)CacheHelper.cs類,代碼如下:
using System; using System.Web; using System.Collections; using System.Web.Caching; public class CacheHelper { /// <summary> /// 獲取數(shù)據(jù)緩存 /// </summary> /// <param name="cacheKey">鍵</param> public static object GetCache(string cacheKey) { var objCache = HttpRuntime.Cache.Get(cacheKey); return objCache; } /// <summary> /// 設(shè)置數(shù)據(jù)緩存 /// </summary> public static void SetCache(string cacheKey, object objObject) { var objCache = HttpRuntime.Cache; objCache.Insert(cacheKey, objObject); } /// <summary> /// 設(shè)置數(shù)據(jù)緩存 /// </summary> public static void SetCache(string cacheKey, object objObject, int timeout = 7200) { try { if (objObject == null) return; var objCache = HttpRuntime.Cache; //相對(duì)過(guò)期 //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null); //絕對(duì)過(guò)期時(shí)間 objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null); } catch (Exception) { //throw; } } /// <summary> /// 移除指定數(shù)據(jù)緩存 /// </summary> public static void RemoveAllCache(string cacheKey) { var cache = HttpRuntime.Cache; cache.Remove(cacheKey); } /// <summary> /// 移除全部緩存 /// </summary> public static void RemoveAllCache() { var cache = HttpRuntime.Cache; var cacheEnum = cache.GetEnumerator(); while (cacheEnum.MoveNext()) { cache.Remove(cacheEnum.Key.ToString()); } } }
然后是調(diào)用:
public IEnumerable<CompanyModel> FindCompanys() { var cache = CacheHelper.GetCache("commonData_Company");//先讀取 if (cache == null)//如果沒有該緩存 { var queryCompany = _base.CompanyModel();//從數(shù)據(jù)庫(kù)取出 var enumerable = queryCompany.ToList(); CacheHelper.SetCache("commonData_Company", enumerable);//添加緩存 return enumerable; } var result = (List<CompanyModel>)cache;//有就直接返回該緩存 return result; }
測(cè)試結(jié)果:
首次加載進(jìn)來(lái)是為null,然后讀取數(shù)據(jù)庫(kù),添加進(jìn)緩存,當(dāng)前返回前臺(tái)的是從數(shù)據(jù)庫(kù)中取出的數(shù)據(jù)。
刷新頁(yè)面,發(fā)現(xiàn)緩存中已經(jīng)有了讀出的30條數(shù)據(jù),
然后接下來(lái)走,返回緩存中的數(shù)據(jù):
以上就是C# Cache緩存讀取的設(shè)置方法的詳細(xì)內(nèi)容,更多關(guān)于C# Cache緩存讀取的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#遞歸方法實(shí)現(xiàn)無(wú)限級(jí)分類顯示效果實(shí)例
這篇文章主要介紹了C#遞歸方法實(shí)現(xiàn)無(wú)限級(jí)分類顯示效果,結(jié)合完整實(shí)例形式分析了C#遞歸算法與數(shù)據(jù)元素遍歷的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06新手小白用C# winform 讀取Excel表的實(shí)現(xiàn)
這篇文章主要介紹了新手小白用C# winform 讀取Excel表的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01winform天氣預(yù)報(bào)小工具(附源碼下載)
主要原理就是利用網(wǎng)上免費(fèi)的webservice獲取天氣數(shù)據(jù),需要的朋友可以參考下2012-03-03C# MVC模式中應(yīng)該怎樣區(qū)分應(yīng)用程序邏輯(Controller層)和業(yè)務(wù)邏輯(Model層)?
這篇文章主要介紹了C# MVC模式中應(yīng)該怎樣區(qū)分應(yīng)用程序邏輯(Controller層)和業(yè)務(wù)邏輯(Model層)?,這也小編做.NET項(xiàng)目時(shí)經(jīng)常思考和讓人混亂的一個(gè)問題,這篇文章寫的挺好,一下清晰了許多,需要的朋友可以參考下2015-06-06C#實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法
應(yīng)人才測(cè)評(píng)產(chǎn)品的需求,導(dǎo)出測(cè)評(píng)報(bào)告是其中一個(gè)重要的環(huán)節(jié),報(bào)告的文件類型也多種多樣,其中WORD輸出也扮演了一個(gè)重要的角色,本文給大家介紹了C#實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法及一些體會(huì),需要的朋友可以參考下2023-10-10