MemoryCache.AddOrGetExisting Method (CacheItem, CacheItemPolicy)

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.

ExceptionCondition
ArgumentNullException

item is null.

ArgumentNullException

The Value property is null.

ArgumentException

An UpdateCallback property has been supplied.

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.

Caution noteCaution

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

Supported in: 4.5.2, 4.5.1, 4.5, 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft