MemoryCache.AddOrGetExisting 方法

定義

將快取項目插入快取區中。Inserts a cache entry into the cache.

多載

AddOrGetExisting(CacheItem, CacheItemPolicy)

使用指定的 CacheItem 執行個體和有關如何收回項目的詳細資料,在快取中加入快取項目。Adds a cache entry into the cache using the specified CacheItem instance and details about how to evict the entry.

AddOrGetExisting(String, Object, DateTimeOffset, String)

使用指定索引鍵、值和絕對逾期值,在快取區中插入快取項目。Adds a cache entry into the cache using the specified key and a value and an absolute expiration value.

AddOrGetExisting(String, Object, CacheItemPolicy, String)

使用指定的索引鍵、值和有關如何收回快取項目的指定詳細資料,在快取區中插入快取項目。Inserts a cache entry into the cache using the specified key and value and the specified details for how it is to be evicted.

備註

AddOrGetExisting 方法多載是用來在快取中插入快取專案。The AddOrGetExisting method overloads are used to insert a cache entry into the cache. 如果具有相符索引鍵的快取專案不存在,這些方法會插入新的專案。If a cache entry with a matching key does not exist, these methods insert a new entry. 如果具有相符索引鍵的快取專案已經存在,則會傳回現有的專案。If a cache entry with a matching key already exists, they return the existing entry.

AddOrGetExisting(CacheItem, CacheItemPolicy)

使用指定的 CacheItem 執行個體和有關如何收回項目的詳細資料,在快取中加入快取項目。Adds a cache entry into the cache using the specified CacheItem instance and details about how to evict the entry.

public:
 override System::Runtime::Caching::CacheItem ^ AddOrGetExisting(System::Runtime::Caching::CacheItem ^ item, System::Runtime::Caching::CacheItemPolicy ^ policy);
public override System.Runtime.Caching.CacheItem AddOrGetExisting (System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy);
override this.AddOrGetExisting : System.Runtime.Caching.CacheItem * System.Runtime.Caching.CacheItemPolicy -> System.Runtime.Caching.CacheItem
Public Overrides Function AddOrGetExisting (item As CacheItem, policy As CacheItemPolicy) As CacheItem

參數

item
CacheItem

要新增的物件。The object to add.

policy
CacheItemPolicy

物件,包含快取項目的收回詳細資料。An object that contains eviction details for the cache entry. 這個物件提供的收回選項比簡單絕對期限要多。This object provides more options for eviction than a simple absolute expiration.

傳回

如果存在具有相同索引鍵的快取項目,則為現有的快取項目,否則為 nullIf a cache entry with the same key exists, the existing cache entry; otherwise, null.

例外狀況

Value 屬性為 nullThe Value property is null.

CacheItemPolicy 物件的絕對和滑動期限值都會設定為不同於 InfiniteAbsoluteExpirationNoSlidingExpiration 欄位之預設值的值。Both the absolute and sliding expiration values for the CacheItemPolicy object are set to values other than the defaults of InfiniteAbsoluteExpiration and NoSlidingExpiration fields. MemoryCache 類別不能根據絕對期限和滑動期限的組合來設定到期原則。The MemoryCache class cannot set expiration policy based on a combination of an absolute expiration and a sliding expiration. 當您使用 MemoryCache 執行個體時,只能明確設定一項到期設定。Only one expiration setting can be explicitly set when you use the MemoryCache instance. 其他到期設定必須設定為 InfiniteAbsoluteExpiration 欄位或 NoSlidingExpiration 欄位。The other expiration setting must be set to InfiniteAbsoluteExpiration field or NoSlidingExpiration field.

SlidingExpiration 屬性設定為小於 Zero 的值。The SlidingExpiration property is set to a value less than Zero.

-或--or- SlidingExpiration 屬性設定為大於一年的値。The SlidingExpiration property is set to a value greater than one year.

-或--or- Priority 屬性不是 CacheItemPriority 列舉的值。The Priority property is not a value of the CacheItemPriority enumeration.

備註

item 參數提供此方法所使用的金鑰和值。The item parameter supplies the key and the value that is used by the method. 如果快取的快取專案與 item 參數的索引鍵具有相同的索引鍵,此方法就會傳回現有的專案做為 CacheItem 實例。If the cache has a cache entry with the same key as the key of the item parameter, the method returns the existing entry as a CacheItem instance. 如果沒有現有的快取專案,方法會使用 item 參數所提供的索引鍵和值,以及 policy所指定的收回詳細資料,建立一個新的。If there is no existing cache entry, the method creates a new one by using the key and value supplied by the item parameter, and with the eviction details specified by policy.

警告

AddAddOrGetExisting 方法多載不支援 UpdateCallback 屬性。The Add and AddOrGetExisting method overloads do not support the UpdateCallback property. 因此,若為快取項目設定 UpdateCallback 屬性,請改用 Set 方法多載。Therefore, to set the UpdateCallback property for a cache entry, use the Set method overloads instead.

AddOrGetExisting(String, Object, DateTimeOffset, String)

使用指定索引鍵、值和絕對逾期值,在快取區中插入快取項目。Adds a cache entry into the cache using the specified key and a value and an absolute expiration value.

public override object AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = default);
override this.AddOrGetExisting : string * obj * DateTimeOffset * string -> obj
Public Overrides Function AddOrGetExisting (key As String, value As Object, absoluteExpiration As DateTimeOffset, Optional regionName As String = null) As Object

參數

key
String

要加入之快取項目的唯一識別項。A unique identifier for the cache entry to add.

value
Object

快取項目的資料。The data for the cache entry.

absoluteExpiration
DateTimeOffset

快取項目到期的固定日期和時間。The fixed date and time at which the cache entry will expire.

regionName
String

快取區中的具名區域,快取項目可以加入至這個區域。A named region in the cache to which a cache entry can be added. 請勿傳遞這個參數的值。Do not pass a value for this parameter. 這個參數預設為 null,因為 MemoryCache 類別沒有實作區域。This parameter is null by default, because the MemoryCache class does not implement regions.

傳回

如果存在具有相同索引鍵的快取項目,則為現有的快取項目,否則為 nullIf a cache entry with the same key exists, the existing cache entry; otherwise, null.

例外狀況

value 不是 nullvalue is not null.

regionName 不是 nullregionName is not null.

CacheItemPolicy 物件的絕對和滑動期限值都會設定為不同於 InfiniteAbsoluteExpirationNoSlidingExpiration 欄位之預設值的值。Both the absolute and sliding expiration values for the CacheItemPolicy object are set to values other than the defaults of InfiniteAbsoluteExpiration and NoSlidingExpiration fields. MemoryCache 類別不能根據絕對期限和滑動期限的組合來設定到期原則。The MemoryCache class cannot set expiration policy based on a combination of an absolute expiration and a sliding expiration. 當您使用 MemoryCache 執行個體時,只能明確設定一項到期設定。Only one expiration setting can be explicitly set when you use the MemoryCache instance. 將其他到期設定必須設為 InfiniteAbsoluteExpirationNoSlidingExpirationThe other expiration setting must be set to InfiniteAbsoluteExpiration or NoSlidingExpiration

SlidingExpiration 屬性設定為小於 Zero 的值。The SlidingExpiration property is set to a value less than Zero.

-或--or- SlidingExpiration 屬性設定為大於一年的値。The SlidingExpiration property is set to a value greater than one year.

-或--or- Priority 屬性不是 CacheItemPriority 列舉的值。The Priority property is not a value of the CacheItemPriority enumeration.

備註

如果快取中沒有索引鍵符合 key 參數的快取專案,就會建立新的快取專案,而 MemoryCache.AddOrGetExisting 方法多載會傳回 nullIf the cache does not have a cache entry whose key matches the key parameter, a new cache entry is created, and the MemoryCache.AddOrGetExisting method overload returns null. 如果符合的快取專案存在,則會傳回現有的專案。If a matching cache entry exists, the existing entry is returned.

警告

AddAddOrGetExisting 方法多載不支援 UpdateCallback 屬性。The Add and AddOrGetExisting method overloads do not support the UpdateCallback property. 因此,若為快取項目設定 UpdateCallback 屬性,請改用 Set 方法多載。Therefore, to set the UpdateCallback property for a cache entry, use the Set method overloads instead.

AddOrGetExisting(String, Object, CacheItemPolicy, String)

使用指定的索引鍵、值和有關如何收回快取項目的指定詳細資料,在快取區中插入快取項目。Inserts a cache entry into the cache using the specified key and value and the specified details for how it is to be evicted.

public override object AddOrGetExisting (string key, object value, System.Runtime.Caching.CacheItemPolicy policy, string regionName = default);
override this.AddOrGetExisting : string * obj * System.Runtime.Caching.CacheItemPolicy * string -> obj
Public Overrides Function AddOrGetExisting (key As String, value As Object, policy As CacheItemPolicy, Optional regionName As String = null) As Object

參數

key
String

要加入或取得之快取項目的唯一識別項。A unique identifier for the cache entry to add or get.

value
Object

快取項目的資料。The data for the cache entry.

policy
CacheItemPolicy

物件,包含快取項目的收回詳細資料。An object that contains eviction details for the cache entry. 這個物件提供的收回選項比簡單絕對期限要多。This object provides more options for eviction than a simple absolute expiration.

regionName
String

快取區中的具名區域,快取項目可以加入至這個區域。A named region in the cache to which a cache entry can be added. 請勿傳遞這個參數的值。Do not pass a value for this parameter. 這個參數預設為 null,因為 MemoryCache 類別沒有實作區域。By default, this parameter is null, because the MemoryCache class does not implement regions.

傳回

如果符合的快取項目已經存在,則為快取項目,否則為 nullIf a matching cache entry already exists, a cache entry; otherwise, null.

例外狀況

valuenullvalue is null.

CacheItemPolicy 物件的絕對和滑動期限值都會設定為不同於 InfiniteAbsoluteExpirationNoSlidingExpiration 欄位之預設值的值。Both the absolute and sliding expiration values of CacheItemPolicy object are set to values other than the defaults of InfiniteAbsoluteExpiration and NoSlidingExpiration. MemoryCache 類別不能根據絕對期限和滑動期限兩者的組合來設定到期原則。The MemoryCache class cannot set expiration policy based on a combination of both an absolute and a sliding expiration. 當您使用 MemoryCache 類別時,只能明確設定一個到期設定。Only one expiration setting can be explicitly set when you use the MemoryCache class. 其他設定必須設定為 InfiniteAbsoluteExpirationNoSlidingExpirationThe other setting must be set to InfiniteAbsoluteExpiration or NoSlidingExpiration.

-或--or- 已經對 CacheItemPolicy 指定移除回呼和更新回呼兩者。Both the removal callback and the update callback have been specified for CacheItemPolicy. MemoryCache 只支援對每個快取項目使用一種回呼。The MemoryCache only supports using one type of callback per cache entry.

SlidingExpiration 屬性設定為小於 Zero 的值。The SlidingExpiration property is set to a value less than Zero.

-或--or- SlidingExpiration 已設定為大於一年的值。The SlidingExpiration has been set to a value greater than one year.

-或--or- Priority 屬性不是 CacheItemPriority 列舉的值。The Priority property is not a value of the CacheItemPriority enumeration.

備註

警告

AddAddOrGetExisting 方法多載不支援 UpdateCallback 屬性。The Add and AddOrGetExisting method overloads do not support the UpdateCallback property. 因此,若為快取項目設定 UpdateCallback 屬性,請改用 Set 方法多載。Therefore, to set the UpdateCallback property for a cache entry, use the Set method overloads instead.

適用於