Cache.Add(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) 方法


使用相依性、期限和優先權原則,將指定項目加入 Cache 物件,並且您也可以從 Cache 移除插入項目時使用委派 (Delegate) 來告知您的應用程式。Adds the specified item to 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.

 System::Object ^ Add(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 object Add (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.Add : string * obj * System.Web.Caching.CacheDependency * DateTime * TimeSpan * System.Web.Caching.CacheItemPriority * System.Web.Caching.CacheItemRemovedCallback -> obj
Public Function Add (key As String, value As Object, dependencies As CacheDependency, absoluteExpiration As DateTime, slidingExpiration As TimeSpan, priority As CacheItemPriority, onRemoveCallback As CacheItemRemovedCallback) As Object



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


加入至快取的項目。The item to be added to the cache.


項目的檔案或快取索引鍵相依性。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.


新增的物件過期並從快取中移除的時間。The time at which the added object expires and is removed from the cache. 如果您使用 Sliding Expiration,則 absoluteExpiration 參數必須為 NoAbsoluteExpirationIf you are using sliding expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.


上次存取加入物件的時間與該物件到期的時間之間隔。The interval between the time the added object was last accessed and the time at which that object expires. 如果這個值等於二十分鐘,那麼物件會在最後存取的二十分鐘後過期,並從快取中移除。If this value is the equivalent of 20 minutes, the object expires and is removed from the cache 20 minutes after it is last accessed. 如果您是使用 Absolute Expiration,則 slidingExpiration 參數必須為 NoSlidingExpirationIf you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.


物件的相對花費,如 CacheItemPriority 列舉型別所示。The relative cost of the object, as expressed by the CacheItemPriority enumeration. 在其清除物件時,快取會使用這個值;花費較低的物件會在花費較高的物件之前從快取移除。The cache uses this value when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.


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


如果加入的項目先前已儲存在快取中,則為表示此項目的物件,否則為 nullAn object that represents the item that was added if the item was previously stored in the cache; otherwise, null.


keyvalue 參數是設定為 nullThe key or value parameter is set to null.

slidingExpiration 參數設定為小於 TimeSpan.Zero 或多於一年。The slidingExpiration parameter is set to less than TimeSpan.Zero or 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.


下列範例會建立AddItemToCache方法。The following example creates an AddItemToCache method. 呼叫這個方法時itemRemoved , 它會將屬性設定為false , 並向onRemove CacheItemRemovedCallback委派的新實例註冊方法。When this method is called, it sets an itemRemoved property to false and registers an onRemove method with a new instance of the CacheItemRemovedCallback delegate. 委派的簽章會用於RemovedCallback方法中。The delegate's signature is used in the RemovedCallback method. 然後方法會檢查與快取中的Key1索引鍵相關聯的值。 AddItemToCacheThe AddItemToCache method then checks the value associated with the Key1 key in the cache. 如果值為, nullAdd方法會將專案放在快取中, 其中的Key1索引鍵為, Value 1值為, 絕對到期時間為60秒, 而快取記憶體優先順序高。If the value is null, the Add method places an item in the cache with a key of Key1, a value of Value 1, an absolute expiration of 60 seconds, and a high cache priority. 它也會使用onRemove方法做為引數。It also uses the onRemove method as an argument. 這可讓RemovedCallback您在從快取中移除此專案時呼叫方法。This allows the RemovedCallback method to be called when this item is removed from the cache.


如需如何使用CacheDependency類別CacheItemRemovedCallback和委派的範例, 請參閱快取應用程式資料For examples of how to use the CacheDependency class and the CacheItemRemovedCallback delegate, see Caching Application Data.

public void AddItemToCache(Object sender, EventArgs e) {
    itemRemoved = false;

    onRemove = new CacheItemRemovedCallback(this.RemovedCallback);

    if (Cache["Key1"] == null)
      Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
Public Sub AddItemToCache(sender As Object, e As EventArgs)
    itemRemoved = false

    onRemove = New CacheItemRemovedCallback(AddressOf Me.RemovedCallback)

    If (IsNothing(Cache("Key1"))) Then
      Cache.Add("Key1", "Value 1", Nothing, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove)
    End If
End Sub


如果具有相同key參數的專案已經儲存Cache在中, 此方法的呼叫將會以無訊息模式失敗。Calls to this method will fail silently if an item with the same key parameter is already stored in the Cache. 若要使用相同Cache key的參數覆寫現有的專案, Insert請使用方法。To overwrite an existing Cache item using the same key parameter, use the Insert method.

您不能同時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 item was last accessed, you set the slidingExpiration parameter to the expiration interval, and the absoluteExpiration parameter to NoAbsoluteExpiration.