HOW TO:將項目加入至快取
更新:2007 年 11 月
您可以使用 Cache 物件,存取應用程式快取中的項目。也可以使用 Cache 物件的 Insert 方法,將項目加入至應用程式快取。這個方法可將項目加入至快取並有數個多載,可讓您加入擁有不同選項的項目,以供設定相依性、期限和移除通知。如果使用 Insert 方法將項目加入至快取,但快取中已有相同名稱的項目,則會取代快取中現有的項目。
您也可以使用 Add 方法,將項目加入至快取。這個方法可讓您將所有相同的選項設為 Insert 方法,不過,Add 方法會傳回您加入至快取的物件。此外,如果您使用 Add 方法但是在快取中已有相同名稱的項目,則方法將不會取代項目且不會引發例外狀況。
本主題中的程序會提供下列方法,將項目加入至應用程式快取:
直接透過索引鍵和數值設定項目,即可將項目加入至快取。
使用 Insert 方法將項目加入至快取。
將項目加入至快取並加入相依性,這樣在變更相依性時就可以從快取移除項目。您可以根據其他快取項目、檔案和多個物件設定相依性。
將具有到期原則的項目加入至快取。除了可以設定項目的相依性以外,也可以設定項目在一段時間之後 (滑動期限 (Sliding Expiration)) 或在指定的時間 (絕對期限 (Absolute Expiration)) 時到期。您可以定義絕對期限 (Absolute Expiration) 或滑動期限 (Sliding Expiration),但不可兩者皆設定。
將項目加入至快取,並定義已快取項目的相對優先權。相對優先權可協助 .NET Framework 判斷要移除的快取項目,會先從快取中移除較低優先權的項目,然後移除較高優先權的項目。
呼叫 Add 方法以加入項目。
除了此處顯示的相依性以外,您可以在 SQL Server 資料表上或根據自訂相依性建立相依性。如需詳細資訊,請參閱 ASP.NET 快取概觀和以 SqlCacheDependency 類別在 ASP.NET 中快取。
您也可以使用 CacheItemRemovedCallback 委派 (Delegate),在從快取中移除項目時,讓應用程式快取通知應用程式。如需完整範例,請參閱 HOW TO:當項目從快取移除時告知應用程式。
若要直接透過索引鍵和值設定項目,將項目加入至快取
與您將項目加入字典的方法一樣,指定項目的索引鍵和值,即可將項目加入至快取。
下列程式碼範例會將名為 CacheItem1 的項目加入至 Cache 物件:
Cache["CacheItem1"] = "Cached Item 1";
Cache("CacheItem1") = "Cached Item 1"
若要使用 Insert 方法,將項目加入至快取
呼叫 Insert 方法,傳遞要加入的項目索引鍵和值。
下列程式碼範例會在 CacheItem2 名稱下加入字串:
Cache.Insert("CacheItem2", "Cached Item 2");
Cache.Insert("CacheItem2", "Cached Item 2")
若要透過指定相依性,將項目加入至快取
呼叫 Insert 方法,傳遞給它 CacheDependency 物件的執行個體。
下列程式碼範例會加入名為 CacheItem3 的項目,這個項目會與 CacheItem2 快取中的其他項目相關:
string[] dependencies = { "CacheItem2" }; Cache.Insert("CacheItem3", "Cached Item 3", new System.Web.Caching.CacheDependency(null, dependencies));
Dim dependencies As String() = {"CacheItem2"} Cache.Insert("CacheItem3", "Cached Item 3", _ New System.Web.Caching.CacheDependency( _ Nothing, dependencies))
下列程式碼範例會顯示加入至快取且名為 CacheItem4 的項目,並在 XMLFile.xml 檔案上已設定檔案相依性:
Cache.Insert("CacheItem4", "Cached Item 4", new System.Web.Caching.CacheDependency( Server.MapPath("XMLFile.xml")));
Cache.Insert("CacheItem4", "Cached Item 4", _ New System.Web.Caching.CacheDependency( _ Server.MapPath("XMLFile.xml")))
下列程式碼範例會顯示如何建立多個相依性。這個範例會在 CacheItem1 快取的其他項目上加入索引鍵相依性,並在 XMLFile.xml 檔案上加入檔案相依性。
System.Web.Caching.CacheDependency dep1 = new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")); string[] keyDependencies2 = { "CacheItem1" }; System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency(null, keyDependencies2); System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency(); aggDep.Add(dep1); aggDep.Add(dep2); Cache.Insert("CacheItem5", "Cached Item 5", aggDep);
Dim dep1 As CacheDependency = _ New CacheDependency(Server.MapPath("XMLFile.xml")) Dim keyDependencies2 As String() = {"CacheItem1"} Dim dep2 As CacheDependency = _ New System.Web.Caching.CacheDependency(Nothing, _ keyDependencies2) Dim aggDep As AggregateCacheDependency = _ New System.Web.Caching.AggregateCacheDependency() aggDep.Add(dep1) aggDep.Add(dep2) Cache.Insert("CacheItem5", "Cached Item 5", aggDep)
若要將項目加入至具有到期原則的快取
呼叫 Insert 方法,對此方法傳遞絕對或滑動期限。
下列程式碼範例會將項目加入至其絕對期限為一分鐘的快取中:
Cache.Insert("CacheItem6", "Cached Item 6", null, DateTime.Now.AddMinutes(1d), System.Web.Caching.Cache.NoSlidingExpiration);
Cache.Insert("CacheItem6", "Cached Item 6", _ Nothing, DateTime.Now.AddMinutes(1.0), _ TimeSpan.Zero)
下列程式碼範例會將項目加入至其滑動期限為 10 分鐘的快取中:
Cache.Insert("CacheItem7", "Cached Item 7", null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0));
Cache.Insert("CacheItem7", "Cached Item 7", _ Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, _ New TimeSpan(0, 10, 0))
若要使用優先權設定,將項目加入快取
呼叫 Insert 方法,從 CacheItemPriority 列舉型別 (Enumeration) 指定值。
下列程式碼範例會將項目加入至其優先權值為 High 的快取中:
Cache.Insert("CacheItem8", "Cached Item 8", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
Cache.Insert("CacheItem8", "Cached Item 8", _ Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, _ System.Web.Caching.Cache.NoSlidingExpiration, _ System.Web.Caching.CacheItemPriority.High, _ Nothing)
若要使用 Add 方法,將項目加入至快取
呼叫 Add 方法,會傳回表示項目的物件。
下列程式碼範例會將項目加入至名為 CacheItem9 的快取,並將變數 CachedItem9 的值設為已加入的項目。
string CachedItem9 = (string)Cache.Add("CacheItem9", "Cached Item 9", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
Dim CachedItem9 As String = CStr(Cache.Add("CacheItem9", _ "Cached Item 9", Nothing, _ System.Web.Caching.Cache.NoAbsoluteExpiration, _ System.Web.Caching.Cache.NoSlidingExpiration, _ System.Web.Caching.CacheItemPriority.Default, _ Nothing))