Cache.Insert 方法

定義

插入項目至 Cache 物件。Inserts an item into the Cache object. 使用這個方法的其中一個版本以相同 key 參數來覆寫現有 Cache 項目。Use one of the versions of this method to overwrite an existing Cache item with the same key parameter.

多載

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)

使用相依性、期限和優先權原則,將指定項目插入 Cache 物件,並且您也可以從 Cache 移除插入項目時使用委派來告知您的應用程式。Inserts an object into the Cache object with dependencies, expiration and priority policies, and a delegate you can use to notify your application when the inserted item is removed from the Cache.

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)

從快取區移除項目之前,將物件連同相依性、期限原則及可用來通知應用程式的委派一併插入 Cache 物件中。Inserts an object into the Cache object together with dependencies, expiration policies, and a delegate that you can use to notify the application before the item is removed from the cache.

Insert(String, Object, CacheDependency)

將物件插入至有檔案或索引鍵相依性的 CacheInserts an object into the Cache that has file or key dependencies.

Insert(String, Object)

使用 Cache 列舉型別所提供的預設值,將項目插入 CacheItemPriority 物件,並讓快取索引鍵參考其位置。Inserts an item into the Cache object with a cache key to reference its location, using default values provided by the CacheItemPriority enumeration.

Insert(String, Object, CacheDependency, DateTime, TimeSpan)

使用相依性和期限原則將物件插入 CacheInserts an object into the Cache with dependencies and expiration policies.

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)

使用相依性、期限和優先權原則,將指定項目插入 Cache 物件,並且您也可以從 Cache 移除插入項目時使用委派來告知您的應用程式。Inserts an object into the Cache object with dependencies, expiration and priority policies, and a delegate you can use to notify your application when the inserted item is removed from the Cache.

public:
 void Insert(System::String ^ key, System::Object ^ value, System::Web::Caching::CacheDependency ^ dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System::Web::Caching::CacheItemPriority priority, System::Web::Caching::CacheItemRemovedCallback ^ onRemoveCallback);
public void Insert (string key, object value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System.Web.Caching.CacheItemPriority priority, System.Web.Caching.CacheItemRemovedCallback onRemoveCallback);
member this.Insert : string * obj * System.Web.Caching.CacheDependency * DateTime * TimeSpan * System.Web.Caching.CacheItemPriority * System.Web.Caching.CacheItemRemovedCallback -> unit
Public Sub Insert (key As String, value As Object, dependencies As CacheDependency, absoluteExpiration As DateTime, slidingExpiration As TimeSpan, priority As CacheItemPriority, onRemoveCallback As CacheItemRemovedCallback)

參數

key
String

用來參考物件的快取索引鍵。The cache key used to reference the object.

value
Object

要插入至快取的物件。The object to be inserted in the cache.

dependencies
CacheDependency

項目的檔案或快取索引鍵相依性。The file or cache key dependencies for the item. 任何相依性變更時,物件會變成無效並從快取中移除。When any dependency changes, the object becomes invalid and is removed from the cache. 如果沒有相依性,這個參數就會包含 nullIf there are no dependencies, this parameter contains null.

absoluteExpiration
DateTime

插入物件過期並從快取移除的時間。The time at which the inserted object expires and is removed from the cache. 為避免當地時間發生問題 (例如,從標準時間變更為日光節約時間),請使用 UtcNow 而非 Now 做為這個參數值。To avoid possible issues with local time such as changes from standard time to daylight saving time, use UtcNow rather than Now for this parameter value. 如果您是使用 Absolute Expiration,則 slidingExpiration 參數必須為 NoSlidingExpirationIf you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.

slidingExpiration
TimeSpan

上次存取插入物件的時間與該物件到期的時間之間隔。The interval between the time the inserted object was last accessed and the time at which that object expires. 如果此值相當於 20 分鐘,物件將會到期,而且會從上次存取之後的 20 分鐘從快取中移除。If this value is the equivalent of 20 minutes, the object will expire and be removed from the cache 20 minutes after it was last accessed. 如果您使用 Sliding Expiration,則 absoluteExpiration 參數必須為 NoAbsoluteExpirationIf you are using sliding expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.

priority
CacheItemPriority

與儲存在快取中其他項目相關的物件之花費,如 CacheItemPriority 列舉型別所示。The cost of the object relative to other items stored in the cache, as expressed by the CacheItemPriority enumeration. 當快取清除物件時會用到這個值;花費較低的物件會在花費較高的物件之前從快取中移除。This value is used by the cache when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.

onRemoveCallback
CacheItemRemovedCallback

當物件從快取移除時,將會呼叫委派 (如果有提供的話)。A delegate that, if provided, will be called when an object is removed from the cache. 您可以使用這個在它們的物件從快取刪除時告知應用程式。You can use this to notify applications when their objects are deleted from the cache.

例外狀況

keyvalue 參數為 nullThe key or value parameter is null.

請將 slidingExpiration 參數設定為小於 TimeSpan.Zero,或等於一年以上。You set the slidingExpiration parameter to less than TimeSpan.Zero or the equivalent of more than one year.

您要為 Cache 加入的項目都會設定 absoluteExpirationslidingExpiration 參數。The absoluteExpiration and slidingExpiration parameters are both set for the item you are trying to add to the Cache.

範例

下列範例示範當您將專案插入應用程式的Cache物件時, 如何指派高優先權。The following example demonstrates how to assign an item high priority when you insert it into your application's Cache object.

注意

如需如何搭配使用這個方法與CacheItemRemovedCallback委派的詳細資訊, 請參閱如何:從快取中移除專案時, 通知應用程式。For more information about how to use this method with the CacheItemRemovedCallback delegate, see How to: Notify an Application When an Item Is Removed from the Cache.

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero, CacheItemPriority.High, onRemove);
   
Cache.Insert("DSN", connectionString, Nothing, DateTime.Now.AddMinutes(2), TimeSpan.Zero, CacheItemPriority.High, onRemove)

備註

這個方法會覆寫具有Cache相同key參數的現有專案。This method will overwrite an existing Cache item with the same key parameter.

您不能同時absoluteExpiration設定和slidingExpiration參數。You cannot set both the absoluteExpiration and slidingExpiration parameters. 如果您想要快取專案在特定時間過期, 請將absoluteExpiration參數設定為特定時間, slidingExpiration並將參數設為NoSlidingExpirationIf you intend the cache item to expire at a specific time, you set the absoluteExpiration parameter to the specific time, and the slidingExpiration parameter to NoSlidingExpiration.

如果您想要快取專案在上次存取專案之後經過一段時間後過期, 請將slidingExpiration參數設為到期時間間隔, absoluteExpiration並將參數設定為NoAbsoluteExpirationIf you intend the cache item to expire after a certain amount of time has passed since the last access to the item, you set the slidingExpiration parameter to the expiration interval, and the absoluteExpiration parameter to NoAbsoluteExpiration.

另請參閱

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)

從快取區移除項目之前,將物件連同相依性、期限原則及可用來通知應用程式的委派一併插入 Cache 物件中。Inserts an object into the Cache object together with dependencies, expiration policies, and a delegate that you can use to notify the application before the item is removed from the cache.

public:
 void Insert(System::String ^ key, System::Object ^ value, System::Web::Caching::CacheDependency ^ dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System::Web::Caching::CacheItemUpdateCallback ^ onUpdateCallback);
public void Insert (string key, object value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System.Web.Caching.CacheItemUpdateCallback onUpdateCallback);
member this.Insert : string * obj * System.Web.Caching.CacheDependency * DateTime * TimeSpan * System.Web.Caching.CacheItemUpdateCallback -> unit
Public Sub Insert (key As String, value As Object, dependencies As CacheDependency, absoluteExpiration As DateTime, slidingExpiration As TimeSpan, onUpdateCallback As CacheItemUpdateCallback)

參數

key
String

用來參考物件的快取索引鍵。The cache key that is used to reference the object.

value
Object

要插入至快取中的物件。The object to insert into the cache.

dependencies
CacheDependency

項目的檔案或快取索引鍵相依性。The file or cache key dependencies for the item. 任何相依性變更時,物件會變成無效並從快取中移除。When any dependency changes, the object becomes invalid and is removed from the cache. 如果沒有相依性,這個參數就會包含 nullIf there are no dependencies, this parameter contains null.

absoluteExpiration
DateTime

插入物件過期並從快取移除的時間。The time at which the inserted object expires and is removed from the cache. 為避免當地時間發生問題 (例如,從標準時間變更為日光節約時間),請使用 UtcNow 而非 Now 做為這個參數值。To avoid possible issues with local time such as changes from standard time to daylight saving time, use UtcNow instead of Now for this parameter value. 如果您是使用絕對期限,則 slidingExpiration 參數必須設為 NoSlidingExpirationIf you are using absolute expiration, the slidingExpiration parameter must be set to NoSlidingExpiration.

slidingExpiration
TimeSpan

上次存取快取物件的時間與該物件到期的時間之間隔。The interval between the time that the cached object was last accessed and the time at which that object expires. 如果此值相當於 20 分鐘,物件將會到期,而且會從上次存取之後的 20 分鐘從快取中移除。If this value is the equivalent of 20 minutes, the object will expire and be removed from the cache 20 minutes after it was last accessed. 如果您是使用滑動期限,則 absoluteExpiration 參數必須設為 NoAbsoluteExpirationIf you are using sliding expiration, the absoluteExpiration parameter must be set to NoAbsoluteExpiration.

onUpdateCallback
CacheItemUpdateCallback

從快取中移除物件之前呼叫的委派。A delegate that will be called before the object is removed from the cache. 您可以使用此委派更新快取項目,並且確保該項目不會從快取中移除。You can use this to update the cached item and ensure that it is not removed from the cache.

例外狀況

keyvalueonUpdateCallback 參數是 nullThe key, value, or onUpdateCallback parameter is null.

請將 slidingExpiration 參數設定為小於 TimeSpan.Zero,或等於一年以上。You set the slidingExpiration parameter to less than TimeSpan.Zero or the equivalent of more than one year.

您要為 Cache 加入的項目都會設定 absoluteExpirationslidingExpiration 參數。The absoluteExpiration and slidingExpiration parameters are both set for the item you are trying to add to the Cache.

-或--or- dependencies 參數為 null,而且 absoluteExpiration 參數設為 NoAbsoluteExpirationslidingExpiration 參數則設為 NoSlidingExpirationThe dependencies parameter is null, and the absoluteExpiration parameter is set to NoAbsoluteExpiration, and the slidingExpiration parameter is set to NoSlidingExpiration.

備註

Insert方法可讓您在快取中插入新專案, 並提供在從快取中移除專案之前呼叫的委派。The Insert method enables you to insert a new item in the cache and provide a delegate that is called before the item is removed from the cache. 在委派中, 您可以更新快取的專案, 藉此避免將它從快取中移除。In the delegate, you can update the cached item and thereby prevent it from being removed from the cache.

這個方法會覆寫具有Cache相同key參數的現有專案。This method will overwrite an existing Cache item with the same key parameter.

您不能同時absoluteExpiration設定和slidingExpiration參數。You cannot set both the absoluteExpiration and slidingExpiration parameters. 如果您想要快取專案在特定時間過期, 請將absoluteExpiration參數設定為特定時間, slidingExpiration並將參數設為NoSlidingExpirationIf you intend the cache item to expire at a specific time, you set the absoluteExpiration parameter to the specific time, and the slidingExpiration parameter to NoSlidingExpiration.

如果您想要快取專案在上次存取專案之後的一段時間後過期, 請將slidingExpiration參數設定為到期間隔, 並absoluteExpiration將參數設定為NoAbsoluteExpirationIf you intend the cache item to expire after a set period of time since the item was last accessed, you set the slidingExpiration parameter to the expiration interval, and you set the absoluteExpiration parameter to NoAbsoluteExpiration.

另請參閱

Insert(String, Object, CacheDependency)

將物件插入至有檔案或索引鍵相依性的 CacheInserts an object into the Cache that has file or key dependencies.

public:
 void Insert(System::String ^ key, System::Object ^ value, System::Web::Caching::CacheDependency ^ dependencies);
public void Insert (string key, object value, System.Web.Caching.CacheDependency dependencies);
member this.Insert : string * obj * System.Web.Caching.CacheDependency -> unit
Public Sub Insert (key As String, value As Object, dependencies As CacheDependency)

參數

key
String

用來識別項目的快取索引鍵。The cache key used to identify the item.

value
Object

要插入至快取的物件。The object to be inserted in the cache.

dependencies
CacheDependency

插入物件的檔案或快取索引鍵相依性。The file or cache key dependencies for the inserted object. 任何相依性變更時,物件會變成無效並從快取中移除。When any dependency changes, the object becomes invalid and is removed from the cache. 如果沒有相依性,這個參數就會包含 nullIf there are no dependencies, this parameter contains null.

例外狀況

keyvalue 參數為 nullThe key or value parameter is null.

範例

下列範例示範如何使用 XML 設定檔案的快取相依性, 將專案插入應用程式的快取中。The following example demonstrates how to insert an item into an application's cache with a cache dependency on an XML configuration file.

Cache.Insert("DSN", connectionString, new CacheDependency(Server.MapPath("myconfig.xml")));
   
Cache.Insert("DSN", connectionString, New CacheDependency(Server.MapPath("myconfig.xml")))

備註

這個方法會覆寫索引鍵符合key參數的現有快取專案。This method will overwrite an existing cache item whose key matches the key parameter. 使用Insert方法的這個多載加入至快取的物件會插入, 但不含檔案或快取相依性Default、的優先順序、的滑動NoSlidingExpiration到期值, 以及的絕對到期NoAbsoluteExpiration值。The object added to the cache using this overload of the Insert method is inserted with no file or cache dependencies, a priority of Default, a sliding expiration value of NoSlidingExpiration, and an absolute expiration value of NoAbsoluteExpiration.

另請參閱

Insert(String, Object)

使用 Cache 列舉型別所提供的預設值,將項目插入 CacheItemPriority 物件,並讓快取索引鍵參考其位置。Inserts an item into the Cache object with a cache key to reference its location, using default values provided by the CacheItemPriority enumeration.

public:
 void Insert(System::String ^ key, System::Object ^ value);
public void Insert (string key, object value);
member this.Insert : string * obj -> unit
Public Sub Insert (key As String, value As Object)

參數

key
String

用來參考項目的快取索引鍵。The cache key used to reference the item.

value
Object

要插入至快取的物件。The object to be inserted into the cache.

例外狀況

keyvalue 參數為 nullThe key or value parameter is null.

範例

下列範例示範如何將專案插入應用程式的快取中。The following example demonstrates how to insert an item into an application's cache.

Cache.Insert("DSN", connectionString);
   
Cache.Insert("DSN", connectionString)

備註

這個方法會覆寫索引鍵符合key參數的現有快取專案。This method will overwrite an existing cache item whose key matches the key parameter. 使用Insert方法的這個多載加入至快取的物件會插入, 但不含檔案或快取相依性Default、的優先順序、的滑動NoSlidingExpiration到期值, 以及的絕對到期NoAbsoluteExpiration值。The object added to the cache using this overload of the Insert method is inserted with no file or cache dependencies, a priority of Default, a sliding expiration value of NoSlidingExpiration, and an absolute expiration value of NoAbsoluteExpiration.

另請參閱

Insert(String, Object, CacheDependency, DateTime, TimeSpan)

使用相依性和期限原則將物件插入 CacheInserts an object into the Cache with dependencies and expiration policies.

public:
 void Insert(System::String ^ key, System::Object ^ value, System::Web::Caching::CacheDependency ^ dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);
public void Insert (string key, object value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);
member this.Insert : string * obj * System.Web.Caching.CacheDependency * DateTime * TimeSpan -> unit
Public Sub Insert (key As String, value As Object, dependencies As CacheDependency, absoluteExpiration As DateTime, slidingExpiration As TimeSpan)

參數

key
String

用來參考物件的快取索引鍵。The cache key used to reference the object.

value
Object

要插入至快取的物件。The object to be inserted in the cache.

dependencies
CacheDependency

插入物件的檔案或快取索引鍵相依性。The file or cache key dependencies for the inserted object. 任何相依性變更時,物件會變成無效並從快取中移除。When any dependency changes, the object becomes invalid and is removed from the cache. 如果沒有相依性,這個參數就會包含 nullIf there are no dependencies, this parameter contains null.

absoluteExpiration
DateTime

插入物件過期並從快取移除的時間。The time at which the inserted object expires and is removed from the cache. 為避免當地時間發生問題 (例如,從標準時間變更為日光節約時間),請使用 UtcNow 而非 Now 做為這個參數值。To avoid possible issues with local time such as changes from standard time to daylight saving time, use UtcNow rather than Now for this parameter value. 如果您是使用 Absolute Expiration,則 slidingExpiration 參數必須為 NoSlidingExpirationIf you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.

slidingExpiration
TimeSpan

在上次存取所插入物件的時間以及該物件到期時間之間的間隔。The interval between the time the inserted object is last accessed and the time at which that object expires. 如果此值相當於 20 分鐘,物件將會到期,而且會從上次存取之後的 20 分鐘從快取中移除。If this value is the equivalent of 20 minutes, the object will expire and be removed from the cache 20 minutes after it was last accessed. 如果您使用 Sliding Expiration,則 absoluteExpiration 參數必須為 NoAbsoluteExpirationIf you are using sliding expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.

例外狀況

keyvalue 參數為 nullThe key or value parameter is null.

請將 slidingExpiration 參數設定為小於 TimeSpan.Zero,或等於一年以上。You set the slidingExpiration parameter to less than TimeSpan.Zero or the equivalent of more than one year.

您要為 Cache 加入的項目都會設定 absoluteExpirationslidingExpiration 參數。The absoluteExpiration and slidingExpiration parameters are both set for the item you are trying to add to the Cache.

範例

下列範例示範如何將專案插入至應用程式的快取中, 其具有絕對到期日。The following example demonstrates how to insert an item into an application's cache with an absolute expiration.

注意

由於這種形式的Insert方法支援快取相依性, 如果專案沒有相依性, 您必須dependency在參數的null逗號Nothing分隔清單中, 將參數宣告為 (在 Visual Basic 中)。Since this form of the Insert method supports cache dependencies, if the item has no dependencies, you must declare the dependency parameter as null (Nothing in Visual Basic) in the comma-delimited list of parameters.

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);
Cache.Insert("DSN", connectionString, Nothing, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration)

下列範例示範如何將專案插入快取中, 並具有滑動到期日。The following example demonstrates how to insert an item into the cache with a sliding expiration.

Cache.Insert("DSN", connectionString, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));
Cache.Insert("DSN", connectionString, Nothing, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10))

備註

這個方法會覆寫具有Cache相同key參數的現有專案。This method will overwrite an existing Cache item with the same key parameter.

如果參數設定為NoSlidingExpiration, 則會停用滑動到期。 slidingExpirationIf the slidingExpiration parameter is set to NoSlidingExpiration, sliding expiration is disabled. 如果您將參數slidingExpiration設定為 [ Zero大於], absoluteExpiration參數會slidingExpiration設定為Now加上包含在參數中的值。If you set the slidingExpiration parameter to greater than Zero, the absoluteExpiration parameter is set to Now plus the value contained in the slidingExpiration parameter. 如果在absoluteExpiration參數所指定的時間量之前, 從快取要求專案, 專案將會再次放在快取中, 並absoluteExpiration會再次設定為DateTime.Now加上slidingExpiration參數中包含的值。If the item is requested from the cache before the amount of time specified by the absoluteExpiration parameter, the item will be placed in the cache again, and absoluteExpiration will again be set to DateTime.Now plus the value contained in the slidingExpiration parameter. 如果在absoluteExpiration參數中的日期之前, 不要求快取中的專案, 則會從快取中移除專案。If the item is not requested from the cache before the date in the absoluteExpiration parameter, the item is removed from the cache. . 使用 insert 方法的這個多載加入至快取中的專案, 會以的優先順序Default插入。The item added to the cache using this overload of the insert method is inserted with a priority of Default.

另請參閱

適用於