1. 應用程序數據緩存
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();
}
以上幾種方法都很好的解決了數據緩存的問題,但由一個最大的問題是當數據發生變化了,而緩存裏還是過期的數據,只有等緩存過期後纔會重新獲取最新的數據,這樣的話,很多時候用戶獲取的數據都是和實際數據不一致的過期數據。這同樣給用戶造成了比較大的麻煩,怎麼辦呢?接着往下看。
2. 文件緩存依賴
這種策略讓緩存依賴於一個指定的文件,通過改變文件的更新日期來清除緩存。
/// <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>
/// 設置以緩存依賴的方式緩存數據
/// </summary>
/// <param name="CacheKey">索引鍵值</param>
/// <param name="objObject">緩存對象</param>
/// <param name="cacheDepen">依賴對象</param>
public static void SetCache(string CacheKey, object objObject, System.Web.Caching.CacheDependency dep)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(
CacheKey,
objObject,
dep,
System.Web.Caching.Cache.NoAbsoluteExpiration, //從不過期
System.Web.Caching.Cache.NoSlidingExpiration, //禁用可調過期
System.Web.Caching.CacheItemPriority.Default,
null);
}
protected void Page_Load(object sender, EventArgs e)
{
string CacheKey = "cachetest";
object objModel = GetCache(CacheKey);//從緩存中獲取
if (objModel == null) //緩存裏沒有
{
objModel = DateTime.Now;//把當前時間進行緩存
if (objModel != null)
{
//依賴 C://test.txt 文件的變化來更新緩存
System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency("C://test.txt");
SetCache(CacheKey, objModel, dep);//寫入緩存
}
}
Label1.Text = objModel.ToString();
}
當我們改變test.txt的內容時,緩存會自動更新。這種方式非常適合讀取配置文件的緩存處理。如果配置文件不變化,就一直讀取緩存的信息,一旦配置發生變化,自動更新同步緩存的數據。這種方式的缺點是,如果緩存的數據比較多,相關的依賴文件比較鬆散,對管理這些依賴文件有一定的麻煩。對於負載均衡環境下,還需要同時更新多臺Web服務器下的緩存文件,如果多個Web應用中的緩存依賴於同一個共享的文件,可能會省掉這個麻煩。
轉自:http://www.cnblogs.com/ltp/archive/2009/06/30/1514326.html
http://www.cnblogs.com/ltp/archive/2009/06/30/1514327.html