Export (0) Print
Expand All

Cache.Add Method

Adds the specified item to the 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.

[Visual Basic]
Public Function Add( _
   ByVal key As String, _
   ByVal value As Object, _
   ByVal dependencies As CacheDependency, _
   ByVal absoluteExpiration As DateTime, _
   ByVal slidingExpiration As TimeSpan, _
   ByVal priority As CacheItemPriority, _
   ByVal onRemoveCallback As CacheItemRemovedCallback _
) As Object
public object Add(
 string key,
 object value,
 CacheDependency dependencies,
 DateTime absoluteExpiration,
 TimeSpan slidingExpiration,
 CacheItemPriority priority,
 CacheItemRemovedCallback onRemoveCallback
public: Object* Add(
 String* key,
 Object* value,
 CacheDependency* dependencies,
 DateTime absoluteExpiration,
 TimeSpan slidingExpiration,
 CacheItemPriority priority,
 CacheItemRemovedCallback* onRemoveCallback
public function Add(
   key : String,
 value : Object,
 dependencies : CacheDependency,
 absoluteExpiration : DateTime,
 slidingExpiration : TimeSpan,
 priority : CacheItemPriority,
 onRemoveCallback : CacheItemRemovedCallback
) : Object;


The cache key used to reference the item.
The item to be added to the cache.
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 paramter contains a null reference (Nothing in Visual Basic).
The time at which the added object expires and is removed from the cache.
The interval between the time the added object was last accessed and when 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.
The relative cost of the object, as expressed by the 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.
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.

Return Value

An Object if the item was previously stored in the Cache; otherwise a null reference (Nothing in Visual Basic).


Exception Type Condition
ArgumentNullException The key or value parameter is set to a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException The slidingExpiration parameter is set to less than TimeSpan.Zero or more than one year.
ArgumentException The absoluteExpiration and slidingExpiration parameters are both set for the item you are trying to add to the Cache.


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 Insert method.

Note   If the key or value parameter is null, an ArgumentNullException is thrown. If you set the slidingexpiration parameter to less than TimeSpan.Zero, or the equivalent of more than one year, an ArgumentOutOfRangeException is thrown. You cannot set both sliding and absolute expirations on the same cached item. If you do so, an ArgumentException is thrown.


Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Cache Class | Cache Members | System.Web.Caching Namespace | CacheDependency | CacheItemPriority | CacheItemRemovedCallback | Caching Application Data

© 2015 Microsoft