Cache.Insert Method (String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)

Inserts an object into 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.

Namespace: System.Web.Caching
Assembly: System.Web (in system.web.dll)

public:
void Insert (
	String^ key, 
	Object^ value, 
	CacheDependency^ dependencies, 
	DateTime absoluteExpiration, 
	TimeSpan slidingExpiration, 
	CacheItemPriority priority, 
	CacheItemRemovedCallback^ onRemoveCallback
)
public void Insert (
	String key, 
	Object value, 
	CacheDependency dependencies, 
	DateTime absoluteExpiration, 
	TimeSpan slidingExpiration, 
	CacheItemPriority priority, 
	CacheItemRemovedCallback onRemoveCallback
)
public function Insert (
	key : String, 
	value : Object, 
	dependencies : CacheDependency, 
	absoluteExpiration : DateTime, 
	slidingExpiration : TimeSpan, 
	priority : CacheItemPriority, 
	onRemoveCallback : CacheItemRemovedCallback
)
Not applicable.

Parameters

key

The cache key used to reference the object.

value

The object to be inserted in 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 a null reference (Nothing in Visual Basic).

absoluteExpiration

The time at which the inserted object expires and is removed from the cache. To avoid possible issues with local time such as changes from standard time to daylight time, use UtcNow rather than Now for this parameter value. If you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.

slidingExpiration

The interval between the time the inserted object was last accessed and the time at which that object expires. If this value is the equivalent of 20 minutes, the object will expire and be removed from the cache 20 minutes after it was last accessed. If you are using sliding expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.

priority

The cost of the object relative to other items stored in the cache, as expressed by the CacheItemPriority enumeration. This value is used by the cache 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, will be called when an object is removed from the cache. You can use this to notify applications when their objects are deleted from the cache.

Exception typeCondition

ArgumentNullException

The key or value parameter is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

You set the slidingExpiration parameter to less than TimeSpan.Zero or the equivalent of more than one year.

ArgumentException

The absoluteExpiration and slidingExpiration parameters are both set for the item you are trying to add to the Cache.

This method will overwrite an existing Cache item with the same key parameter.

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

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

The following example demonstrates how to assign an item high priority when you insert it into your application's Cache object.

NoteNote:

For more information about how to use this method with the CacheItemRemovedCallback delegate, see How to: Notify an Application When an Item Is Deleted from the Cache.

No code example is currently available or this language may not be supported.
get_Cache().Insert("DSN", connectionString, null, 
    DateTime.get_Now().AddMinutes(2), TimeSpan.Zero, 
    CacheItemPriority.High, onRemove);

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: