This documentation is archived and is not being maintained.

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:
virtual CacheItem^ AddOrGetExisting(
	CacheItem^ item, 
	CacheItemPolicy^ policy
) override

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, nullptr.

ExceptionCondition
ArgumentNullException

item is nullptr.

ArgumentNullException

The Value property is nullptr.

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.

.NET Framework

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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