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

.NET Framework (current version)
 

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 override object AddOrGetExisting(
	string key,
	object value,
	DateTimeOffset absoluteExpiration,
	string regionName = null
)

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

Exception Condition
ArgumentNullException

key is null.

NotSupportedException

regionName is not null.

ArgumentNullException

value is not null.

ArgumentException

An UpdateCallback property has been supplied.

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

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: