The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

CacheDependency Class

Tracks cache dependencies, which can be files, directories, or keys to other objects in your application's Cache. This class cannot be inherited.

For a list of all members of this type, see CacheDependency Members.


[Visual Basic]
NotInheritable Public Class CacheDependency
   Implements IDisposable
public sealed class CacheDependency : IDisposable
public __gc __sealed class CacheDependency : public IDisposable
public class CacheDependency implements IDisposable

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.


When you add an item to the Cache with a dependency, when the dependency changes, the item is automtically removed from the Cache. For example, suppose you add an item to the Cache and make it dependent upon an array of file names. When one of the files in that array changes, the item associated with the array is removed from the cache.

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

When you add an item to an application's Cache object and in doing so define a cache dependency for that item, an instance of the CacheDependency class is created automatically to track changes to the files, keys, or directories you have specified. This helps you avoid losing changes made to the object between the time it is created and the time it is inserted into the Cache. 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).


[Visual Basic, C#] The following example demonstrates using the HasChanged property to determine if 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.

[Visual Basic] 
' 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.")
   Response.Write("<p>The dependency has not changed.")
End If 

// Insert the cache item.
CacheDependency dep = new CacheDependency(fileName, dt);
cache.Insert("key", "value", dep);

// Check whether CacheDependency.HasChanged is true.
if (dep.HasChanged)
  Response.Write("<p>The dependency has changed.");  
else Response.Write("<p>The dependency has not changed.");

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Web.Caching

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

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

See Also

CacheDependency Members | System.Web.Caching Namespace | Cache | Caching Application Data | Insert