MemoryCache.Set Method (String, Object, DateTimeOffset, String)
Inserts a cache entry into the cache by using a key and a value and specifies time-based expiration details.
Assembly: System.Runtime.Caching (in System.Runtime.Caching.dll)
public override void Set( string key, object value, DateTimeOffset absoluteExpiration, string regionName = null )
Parameters
- key
-
Type:
System.String
A unique identifier for the cache entry to insert.
- 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.
| Exception | Condition |
|---|---|
| NotSupportedException | regionName is not null. |
| ArgumentNullException | key is null. -or- Value is null. |
| ArgumentException |
|
| 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-
|
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.
Available since 4.0