AddOrGetExisting Method (CacheItem, CacheItemPolicy)

MemoryCache.AddOrGetExisting Method (CacheItem, CacheItemPolicy)

.NET Framework (current version)
 

Adds a cache entry into the cache using the specified CacheItem instance and details about how to evict the entry.

Namespace:   System.Runtime.Caching
Assembly:  System.Runtime.Caching (in System.Runtime.Caching.dll)

public override CacheItem AddOrGetExisting(
	CacheItem item,
	CacheItemPolicy policy
)

Parameters

item
Type: System.Runtime.Caching.CacheItem

The object to add.

policy
Type: System.Runtime.Caching.CacheItemPolicy

An object that contains eviction details for the cache entry. This object provides more options for eviction than a simple absolute expiration.

Return Value

Type: System.Runtime.Caching.CacheItem

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

Exception Condition
ArgumentNullException

item is null.

ArgumentNullException

The Value property is null.

ArgumentException

An UpdateCallback property has been supplied.

System_CAPS_noteNote

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.

ArgumentException

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.

ArgumentOutOfRangeException

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.

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.

System_CAPS_warningWarning

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.

.NET Framework
Available since 4.0
Return to top
Show:
© 2016 Microsoft