ASP.NET緩存全解析4:應用程序數據緩存
ASP.NET緩存全解析文章索引
- ASP.NET緩存全解析1:緩存的概述
- ASP.NET緩存全解析2:頁面輸出緩存
- ASP.NET緩存全解析3:頁面局部緩存
- ASP.NET緩存全解析4:應用程序數據緩存
- ASP.NET 緩存全解析5:文件緩存依賴
- ASP.NET 緩存全解析6:數據庫緩存依賴
- ASP.NET 緩存全解析7:第三方分布式緩存解決方案 Memcached和Cacheman
System.Web.Caching 命名空間提供用于緩存服務器上常用數據的類。此命名空間包括 Cache 類,該類是一個字典,您可以在其中存儲任意數據對象,如哈希表和數據集。它還為這些對象提供了失效功能,并為您提供了添加和移除這些對象的方法。您還可以添加依賴于其他文件或緩存項的對象,并在從 Cache 對象中移除對象時執行回調以通知應用程序。
/// <summary>
/// 獲取當前應用程序指定CacheKey的Cache對象值
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <returns>返回緩存對象</returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 設置當前應用程序指定CacheKey的Cache對象值
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <param name="objObject">緩存對象</param>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 設置當前應用程序指定CacheKey的Cache對象值
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <param name="objObject">緩存對象</param>
/// <param name="absoluteExpiration">絕對過期時間</param>
/// <param name="slidingExpiration">最后一次訪問所插入對象時與該對象過期時之間的時間間隔</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)//緩存里沒有
{
objModel = DateTime.Now;//把當前時間進行緩存
if (objModel != null)
{
int CacheTime = 30;//緩存時間30秒
SetCache(CacheKey, objModel, DateTime.Now.AddSeconds(CacheTime), TimeSpan.Zero);//寫入緩存
}
}
Label1.Text = objModel.ToString();
}
/// 獲取當前應用程序指定CacheKey的Cache對象值
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <returns>返回緩存對象</returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 設置當前應用程序指定CacheKey的Cache對象值
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <param name="objObject">緩存對象</param>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 設置當前應用程序指定CacheKey的Cache對象值
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <param name="objObject">緩存對象</param>
/// <param name="absoluteExpiration">絕對過期時間</param>
/// <param name="slidingExpiration">最后一次訪問所插入對象時與該對象過期時之間的時間間隔</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)//緩存里沒有
{
objModel = DateTime.Now;//把當前時間進行緩存
if (objModel != null)
{
int CacheTime = 30;//緩存時間30秒
SetCache(CacheKey, objModel, DateTime.Now.AddSeconds(CacheTime), TimeSpan.Zero);//寫入緩存
}
}
Label1.Text = objModel.ToString();
}
以上幾種方法都很好的解決了數據緩存的問題,但由一個最大的問題是當數據發生變化了,而緩存里還是過期的數據,只有等緩存過期后才會重新獲取最新的數據,這樣的話,很多時候用戶獲取的數據都是和實際數據不一致的過期數據。這同樣給用戶造成了比較大的麻煩,怎么辦呢?接著往下看。
全站熱搜