System.Web.Caching.Cache.Add Method

Adds the specified item to the System.Web.Caching.Cache object with dependencies, expiration and priority policies, and a delegate you can use to notify your application when the inserted item is removed from the Cache.

Syntax

public object Add (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)

Parameters

key
The cache key used to reference the item.
value
The item to be added to the cache.
dependencies
The file or cache key dependencies for the item. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this parameter contains null.
absoluteExpiration
The time at which the added object expires and is removed from the cache. If you are using sliding expiration, the absoluteExpiration parameter must be Cache.NoAbsoluteExpiration.
slidingExpiration
The interval between the time the added object was last accessed and the time at which that object expires. If this value is the equivalent of 20 minutes, the object expires and is removed from the cache 20 minutes after it is last accessed. If you are using absolute expiration, the slidingExpiration parameter must be Cache.NoSlidingExpiration.
priority
The relative cost of the object, as expressed by the System.Web.Caching.CacheItemPriority enumeration. The cache uses this value when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.
onRemoveCallback
A delegate that, if provided, is called when an object is removed from the cache. You can use this to notify applications when their objects are deleted from the cache.

Returns

An object that represents the item that was added if the item was previously stored in the cache; otherwise, null.

Remarks

Calls to this method will fail if an item with the same key parameter is already stored in the Cache. To overwrite an existing Cache item using the same key parameter, use the Cache.Insert(string, object) method.

You cannot set both the absoluteExpiration and slidingExpiration parameters. If you intend the cache item to expire at a specific time, you set the absoluteExpiration parameter to the specific time, and the slidingExpiration parameter to Cache.NoSlidingExpiration.

If you intend the cache item to expire after a certain amount of time has passed since the item was last accessed, you set the slidingExpiration parameter to the expiration interval, and the absoluteExpiration parameter to Cache.NoAbsoluteExpiration.

Requirements

Namespace: System.Web.Caching
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0