แก้ไข

แชร์ผ่าน


MemoryCache.AddOrGetExisting Method

Definition

Inserts a cache entry into the cache.

Overloads

AddOrGetExisting(CacheItem, CacheItemPolicy)

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.

Remarks

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)

Source:
MemoryCache.cs
Source:
MemoryCache.cs
Source:
MemoryCache.cs

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

Parameters

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.

Returns

If a cache entry with the same key exists, the existing cache entry; otherwise, null.

Exceptions

The Value property is null.

Both the absolute and sliding expiration values for the CacheItemPolicy object are set to values other than the defaults of InfiniteAbsoluteExpiration and NoSlidingExpiration fields. The MemoryCache class cannot set expiration policy based on a combination of an absolute expiration and a sliding expiration. Only one expiration setting can be explicitly set when you use the MemoryCache instance. The other expiration setting must be set to InfiniteAbsoluteExpiration field or NoSlidingExpiration field.

The SlidingExpiration property is set to a value less than Zero.

-or-

The SlidingExpiration property is set to a value greater than one year.

-or-

The Priority property is not a value of the CacheItemPriority enumeration.

Remarks

The item parameter supplies the key and the value that is used by the method. 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. 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.

Warning

The Add and AddOrGetExisting method overloads do not support the UpdateCallback property. Therefore, to set the UpdateCallback property for a cache entry, use the Set method overloads instead.

Applies to

AddOrGetExisting(String, Object, DateTimeOffset, String)

Source:
MemoryCache.cs
Source:
MemoryCache.cs
Source:
MemoryCache.cs

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 = Nothing) As Object

Parameters

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. This parameter is null by default, because the MemoryCache class does not implement regions.

Returns

If a cache entry with the same key exists, the existing cache entry; otherwise, null.

Exceptions

value is not null.

regionName is not null.

Both the absolute and sliding expiration values for the CacheItemPolicy object are set to values other than the defaults of InfiniteAbsoluteExpiration and NoSlidingExpiration fields. The MemoryCache class cannot set expiration policy based on a combination of an absolute expiration and a sliding expiration. Only one expiration setting can be explicitly set when you use the MemoryCache instance. The other expiration setting must be set to InfiniteAbsoluteExpiration or NoSlidingExpiration

The SlidingExpiration property is set to a value less than Zero.

-or-

The SlidingExpiration property is set to a value greater than one year.

-or-

The Priority property is not a value of the CacheItemPriority enumeration.

Remarks

If 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.

Warning

The Add and AddOrGetExisting method overloads do not support the UpdateCallback property. Therefore, to set the UpdateCallback property for a cache entry, use the Set method overloads instead.

Applies to

AddOrGetExisting(String, Object, CacheItemPolicy, String)

Source:
MemoryCache.cs
Source:
MemoryCache.cs
Source:
MemoryCache.cs

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 = Nothing) As Object

Parameters

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. By default, this parameter is null, because the MemoryCache class does not implement regions.

Returns

If a matching cache entry already exists, a cache entry; otherwise, null.

Exceptions

value is null.

Both the absolute and sliding expiration values of CacheItemPolicy object are set to values other than the defaults of InfiniteAbsoluteExpiration and NoSlidingExpiration. The MemoryCache class cannot set expiration policy based on a combination of both an absolute and a sliding expiration. Only one expiration setting can be explicitly set when you use the MemoryCache class. The other setting must be set to InfiniteAbsoluteExpiration or NoSlidingExpiration.

-or-

Both the removal callback and the update callback have been specified for CacheItemPolicy. The MemoryCache only supports using one type of callback per cache entry.

The SlidingExpiration property is set to a value less than Zero.

-or-

The SlidingExpiration has been set to a value greater than one year.

-or-

The Priority property is not a value of the CacheItemPriority enumeration.

Remarks

Warning

The Add and AddOrGetExisting method overloads do not support the UpdateCallback property. Therefore, to set the UpdateCallback property for a cache entry, use the Set method overloads instead.

Applies to