Edit

Share via


MemoryCache.Set Method

Definition

Inserts a cache entry into the cache.

Overloads

Set(CacheItem, CacheItemPolicy)

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

Set(String, Object, DateTimeOffset, String)

Inserts a cache entry into the cache by using a key and a value and specifies time-based expiration details.

Set(String, Object, CacheItemPolicy, String)

Inserts a cache entry into the cache by using a key and a value and eviction.

Remarks

If the specified entry does not exist, it is created. If the specified entry exists, it is updated.

Set(CacheItem, CacheItemPolicy)

Source:
MemoryCache.cs
Source:
MemoryCache.cs
Source:
MemoryCache.cs

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

public:
 override void Set(System::Runtime::Caching::CacheItem ^ item, System::Runtime::Caching::CacheItemPolicy ^ policy);
public override void Set (System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy);
override this.Set : System.Runtime.Caching.CacheItem * System.Runtime.Caching.CacheItemPolicy -> unit
Public Overrides Sub Set (item As CacheItem, policy As CacheItemPolicy)

Parameters

item
CacheItem

An object that represents a cache entry to insert.

policy
CacheItemPolicy

An object that contains eviction details for the cache entry. This object provides more options for eviction than a simple absolute expiration.

Exceptions

item is null.

-or-

The Key property is null.

-or-

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.

-or-

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

-or-

The Priority is not a value of the CacheItemPriority enumeration.

Remarks

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.

Applies to

Set(String, Object, DateTimeOffset, String)

Source:
MemoryCache.cs
Source:
MemoryCache.cs
Source:
MemoryCache.cs

Inserts a cache entry into the cache by using a key and a value and specifies time-based expiration details.

public override void Set (string key, object value, DateTimeOffset absoluteExpiration, string regionName = default);
override this.Set : string * obj * DateTimeOffset * string -> unit
Public Overrides Sub Set (key As String, value As Object, absoluteExpiration As DateTimeOffset, Optional regionName As String = Nothing)

Parameters

key
String

A unique identifier for the cache entry to insert.

value
Object

The data for the cache entry.

absoluteExpiration
DateTimeOffset

The fixed date and time at which the cache entry will expire.

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

Exceptions

regionName is not null.

key is null.

-or-

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

  • If both the absolute and sliding expiration values on CacheItemPolicy object are set to values other than the defaults of InfiniteAbsoluteExpiration and NoSlidingExpiration. This occurs because the MemoryCache class does not support expiring entries based on both an absolute and a sliding expiration. Only one expiration setting can be explicitly set when you use the MemoryCache class. The other setting must be set to InfiniteAbsoluteExpiration or NoSlidingExpiration.

  • If both the removal callback and the update callback are specified on CacheItemPolicy object. The MemoryCache class only supports using one type of callback per cache entry.

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-

Remarks

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

The absoluteExpiration parameter indicates when the entry should be removed from the cache.

Removing an entry triggers any associated change monitors. If the removed item was associated with a CacheItemUpdateCallback object or CacheItemRemovedCallback object, the reason for removal that is passed to the callbacks is contained in the Removed property.

Applies to

Set(String, Object, CacheItemPolicy, String)

Source:
MemoryCache.cs
Source:
MemoryCache.cs
Source:
MemoryCache.cs

Inserts a cache entry into the cache by using a key and a value and eviction.

public override void Set (string key, object value, System.Runtime.Caching.CacheItemPolicy policy, string regionName = default);
override this.Set : string * obj * System.Runtime.Caching.CacheItemPolicy * string -> unit
Public Overrides Sub Set (key As String, value As Object, policy As CacheItemPolicy, Optional regionName As String = Nothing)

Parameters

key
String

A unique identifier for the cache entry to insert.

value
Object

The data for the cache entry.

policy
CacheItemPolicy

An object that contains eviction details for the cache entry. This object provides more options for eviction than a simple absolute expiration.

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

Exceptions

key is null.

-or-

value is null

-or-

The callback reference that was passed to the helper method in the UpdateCallback property is null.

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

  • If both the absolute and sliding expiration values on CacheItemPolicy object are set to values other than the defaults of InfiniteAbsoluteExpiration and NoSlidingExpiration. This is because the MemoryCache class does not support expiring entries based on both an absolute and a sliding expiration. Only one expiration setting can be explicitly set when you use the MemoryCache class. The other setting must be set to InfiniteAbsoluteExpiration or NoSlidingExpiration.

  • If both the removal callback and the update callback are specified on CacheItemPolicy class. The MemoryCache class only supports using one type of callback per cache entry.

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.

regionName is not null.

Remarks

Like other Set method overloads, the Set method always puts a cache value in the cache, regardless whether a matching entry already exists. If the specified entry does not exist in the cache, a new cache entry is inserted. If the specified entry exists, it is updated.

Removing an entry triggers any associated change monitors. If the removed item was associated with a CacheItemUpdateCallback object or CacheItemRemovedCallback object, the reason for removal that is passed to the callbacks is contained in the Removed property.

Applies to