This documentation is archived and is not being maintained.
Cache.Add Method
.NET Framework 1.1
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 [C#] public object Add( string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback ); [C++] public: Object* Add( String* key, Object* value, CacheDependency* dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback* onRemoveCallback ); [JScript] public function Add( key : String, value : Object, dependencies : CacheDependency, absoluteExpiration : DateTime, slidingExpiration : TimeSpan, priority : CacheItemPriority, onRemoveCallback : CacheItemRemovedCallback ) : Object;
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 paramter contains a null reference (Nothing in Visual Basic).
- absoluteExpiration
- The time at which the added object expires and is removed from the cache.
- slidingExpiration
- 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.
- priority
- 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.
- 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.
Return Value
An Object if the item was previously stored in the Cache; otherwise a null reference (Nothing in Visual Basic).
Exceptions
| 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. |
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 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.
Requirements
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
Show: