CacheDependency Class
This page is specific to:.NET Framework Version:1.12.03.03.54.0
.NET Framework Class Library
CacheDependency Class

Establishes a dependency relationship between an item stored in an ASP.NET application's Cache object and a file, cache key, an array of either, or another CacheDependency object. The CacheDependency class monitors the dependency relationships so that when any of them changes, the cached item will be automatically removed.

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

'Usage

Dim instance As CacheDependency

'Declaration

<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class CacheDependency _
    Implements IDisposable
Remarks

If you add an item to the cache that is dependent on another object, such as a file or array of files, the dependent item is automatically removed from the cache when the object changes. For example, suppose you create a DataSet object based on data in an XML file. You can add the DataSet to the cache with a CacheDependency object that makes the DataSet dependent on the XML file. If the XML file changes, the DataSet is removed from the cache.

You can add items with dependencies to your application's cache with the Add and Cache..::.Insert methods. You cannot use the Item property to add items to the cache with dependencies.

To set up a dependency, you create an instance of the CacheDependency class, specifying the files, keys, or directories on which the item is dependent, and then pass the dependency to the Add or Cache..::.Insert method. The CacheDependency instance can represent a single file or directory, an array of files or directories, or an array of files or directories along with an array of cache keys (these represent other items stored in the Cache object).

Examples

The following code example demonstrates using the HasChanged property to determine whether a CacheDependency changed since the previous request for an item in the Cache. The dt value passed in the start parameter is set to DateTime..::.Now.

' Insert the cache item.
Dim dep As New CacheDependency(fileName, dt)
myCache.Insert("key", "value", dep)

' Check whether CacheDependency.HasChanged is true.
If dep.HasChanged Then
   Response.Write("<p>The dependency has changed.")
Else
   Response.Write("<p>The dependency has not changed.")
End If 


.NET Framework Security

Inheritance Hierarchy

System..::.Object
  System.Web.Caching..::.CacheDependency
    System.Web.Caching..::.AggregateCacheDependency
    System.Web.Caching..::.SqlCacheDependency
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Other Resources

Community Content

Watch for memory leaks here
Added by:RandomEngy
Careful: if you create a CacheDependency object pointing to a cache item, but never use that CacheDependency object while doing an Insert, that CacheDependency object will stay rooted to the cache item and become a memory leak.
HttpException thrown if directory doesn't exist.
Added by:Paulo Morgado
If a file is to be monitored and the directory where the file should exist doesn't exist, and HttpException will be thrown.
© 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