This documentation is archived and is not being maintained.

MemoryCache::AddOrGetExisting Method (String, Object, DateTimeOffset, String)

Adds a cache entry into the cache using the specified key and a value and an absolute expiration value.

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

public:
virtual Object^ AddOrGetExisting(
	String^ key, 
	Object^ value, 
	DateTimeOffset absoluteExpiration, 
	String^ regionName
) override

Parameters

key
Type: System::String
A unique identifier for the cache entry to add.
value
Type: System::Object
The data for the cache entry.
absoluteExpiration
Type: System::DateTimeOffset
The fixed date and time at which the cache entry will expire.
regionName
Type: System::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 nullptr by default, because the MemoryCache class does not implement regions.

Return Value

Type: System::Object
If a cache entry with the same key exists, the existing cache entry; otherwise, nullptr.

ExceptionCondition
ArgumentNullException

key is nullptr.

NotSupportedException

regionName is not nullptr.

ArgumentNullException

value is not nullptr.

ArgumentException

An UpdateCallback property has been supplied.

NoteNote
The Add and the AddOrGetExisting method overloads do not support the UpdateCallback property. Therefore, to set the UpdateCallback property for a cache entry, use the Set 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 or NoSlidingExpiration

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.

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 nullptr. If a matching cache entry exists, the existing entry is returned.

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