Export (0) Print
Expand All

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

Inserts an object into the Cache with dependencies and expiration policies.

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

public void Insert(
	string key,
	Object value,
	CacheDependency dependencies,
	DateTime absoluteExpiration,
	TimeSpan slidingExpiration
)

Parameters

key
Type: System.String

The cache key used to reference the object.

value
Type: System.Object

The object to be inserted in the cache.

dependencies
Type: System.Web.Caching.CacheDependency

The file or cache key dependencies for the inserted object. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this parameter contains null.

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 rather than Now for this parameter value. If you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.

slidingExpiration
Type: System.TimeSpan

The interval between the time the inserted object is 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.

ExceptionCondition
ArgumentNullException

The key or value parameter is null.

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.

If the slidingExpiration parameter is set to NoSlidingExpiration, sliding expiration is disabled. If you set the slidingExpiration parameter to greater than Zero, the absoluteExpiration parameter is set to Now plus the value contained in the slidingExpiration parameter. If the item is requested from the cache before the amount of time specified by the absoluteExpiration parameter, the item will be placed in the cache again, and absoluteExpiration will again be set to DateTime.Now plus the value contained in the slidingExpiration parameter. If the item is not requested from the cache before the date in the absoluteExpiration parameter, the item is removed from the cache. . The item added to the cache using this overload of the insert method is inserted with a priority of Default.

The following example demonstrates how to insert an item into an application's cache with an absolute expiration.

NoteNote

Since this form of the Insert method supports cache dependencies, if the item has no dependencies, you must declare the dependency parameter as null (Nothing in Visual Basic) in the comma-delimited list of parameters.

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);

The following example demonstrates how to insert an item into the cache with a sliding expiration.

Cache.Insert("DSN", connectionString, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft