Cache..::.Insert Method (String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)
This page is specific to:.NET Framework Version:3.54.0
.NET Framework Class Library
Cache..::.Insert Method (String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)

Updated: April 2009

Inserts an object into the Cache object together with dependencies, expiration policies, and a delegate that you can use to notify the application before the item is removed from the cache.

Namespace:  System.Web.Caching
Assembly:  System.Web (in System.Web.dll)
Syntax

'Usage

Dim instance As Cache
Dim key As String
Dim value As Object
Dim dependencies As CacheDependency
Dim absoluteExpiration As DateTime
Dim slidingExpiration As TimeSpan
Dim onUpdateCallback As CacheItemUpdateCallback

instance.Insert(key, value, dependencies, _
    absoluteExpiration, slidingExpiration, _
    onUpdateCallback)

'Declaration

Public Sub Insert ( _
    key As String, _
    value As Object, _
    dependencies As CacheDependency, _
    absoluteExpiration As DateTime, _
    slidingExpiration As TimeSpan, _
    onUpdateCallback As CacheItemUpdateCallback _
)

Parameters

key
Type: System..::.String
The cache key that is used to reference the object.
value
Type: System..::.Object
The object to insert into the cache.
dependencies
Type: System.Web.Caching..::.CacheDependency
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 nullNothingnullptra null reference (Nothing in Visual Basic).
absoluteExpiration
Type: System..::.DateTime
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 saving time, use UtcNow instead of Now for this parameter value. If you are using absolute expiration, the slidingExpiration parameter must be set to NoSlidingExpiration.
slidingExpiration
Type: System..::.TimeSpan
The interval between the time that the cached 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 set to NoAbsoluteExpiration.
onUpdateCallback
Type: System.Web.Caching..::.CacheItemUpdateCallback
A delegate that will be called before the object is removed from the cache. You can use this to update the cached item and ensure that it is not removed from the cache.
Exceptions

ExceptionCondition
ArgumentNullException

The key, value, or onUpdateCallback parameter is nullNothingnullptra 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.

-or-

The dependencies parameter is nullNothingnullptra null reference (Nothing in Visual Basic), and the absoluteExpiration parameter is set to NoAbsoluteExpiration, and the slidingExpiration parameter is set to NoSlidingExpiration.

Remarks

The Insert method enables you to insert a new item in the cache and provide a delegate that is called before the item is removed from the cache. In the delegate, you can update the cached item and thereby prevent it from being removed from 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 set period of time since the item was last accessed, you set the slidingExpiration parameter to the expiration interval, and you set the absoluteExpiration parameter to NoAbsoluteExpiration.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5 SP1, 3.0 SP2, 2.0 SP2
See Also

Reference

Change History

Date

History

Reason

April 2009

Removed a reference to priority policies in the method summary.

Customer feedback.

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View