MemoryCache.Set Method (CacheItem, CacheItemPolicy)

.NET Framework (current version)

Inserts a cache entry into the cache by using a CacheItem instance to supply the key and value for the cache entry.

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

public override void Set(
	CacheItem item,
	CacheItemPolicy policy


Type: System.Runtime.Caching.CacheItem

An object that represents a cache entry to insert.

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.

Exception Condition

item is null.


The Key property is null.


The Value property is null.


An invalid combination of arguments for the cache entry was passed. This occurs if the following expiration details are set on the policy object for the cache entry:


The SlidingExpiration property is set to a value less than Zero.


The SlidingExpiration property is set to a value greater than one year.


The Priority is not a value of the CacheItemPriority enumeration.

Like other Set method overloads, the Set method always puts a cache value in the cache, regardless whether an entry already exists that has the same key. If the specified entry does not exist in the cache, a new cache entry is inserted. If the specified entry already exists, its value is updated.

.NET Framework
Available since 4.0
Return to top