|
- ASP.NET緩存全解析1:緩存的概述
- ASP.NET緩存全解析2:頁(yè)面輸出緩存
- ASP.NET緩存全解析3:頁(yè)面局部緩存
- ASP.NET緩存全解析4:應(yīng)用程序數(shù)據(jù)緩存
- ASP.NET 緩存全解析5:文件緩存依賴
- ASP.NET 緩存全解析6:數(shù)據(jù)庫(kù)緩存依賴
- ASP.NET 緩存全解析7:第三方分布式緩存解決方案 Memcached和Cacheman
System.Web.Caching 命名空間提供用于緩存服務(wù)器上常用數(shù)據(jù)的類。此命名空間包括 Cache 類,該類是一個(gè)字典,您可以在其中存儲(chǔ)任意數(shù)據(jù)對(duì)象,如哈希表和數(shù)據(jù)集。它還為這些對(duì)象提供了失效功能,并為您提供了添加和移除這些對(duì)象的方法。您還可以添加依賴于其他文件或緩存項(xiàng)的對(duì)象,并在從 Cache 對(duì)象中移除對(duì)象時(shí)執(zhí)行回調(diào)以通知應(yīng)用程序。
/// <summary>
/// 獲取當(dāng)前應(yīng)用程序指定CacheKey的Cache對(duì)象值
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <returns>返回緩存對(duì)象</returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 設(shè)置當(dāng)前應(yīng)用程序指定CacheKey的Cache對(duì)象值
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <param name="objObject">緩存對(duì)象</param>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 設(shè)置當(dāng)前應(yīng)用程序指定CacheKey的Cache對(duì)象值
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <param name="objObject">緩存對(duì)象</param>
/// <param name="absoluteExpiration">絕對(duì)過(guò)期時(shí)間</param>
/// <param name="slidingExpiration">最后一次訪問(wèn)所插入對(duì)象時(shí)與該對(duì)象過(guò)期時(shí)之間的時(shí)間間隔</param>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
protected void Page_Load(object sender, EventArgs e)
{
string CacheKey = "cachetest";
object objModel = GetCache(CacheKey);//從緩存中獲取
if (objModel == null)//緩存里沒(méi)有
{
objModel = DateTime.Now;//把當(dāng)前時(shí)間進(jìn)行緩存
if (objModel != null)
{
int CacheTime = 30;//緩存時(shí)間30秒
SetCache(CacheKey, objModel, DateTime.Now.AddSeconds(CacheTime), TimeSpan.Zero);//寫入緩存
}
}
Label1.Text = objModel.ToString();
}
NET技術(shù):ASP.NET緩存全解析4:應(yīng)用程序數(shù)據(jù)緩存,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。