This topic has not yet been rated - Rate this topic

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)
public class CacheDependency : IDisposable

The CacheDependency type exposes the following members.

  Name Description
Protected method CacheDependency() Initializes a new instance of the CacheDependency class.
Public method CacheDependency(String) Initializes a new instance of the CacheDependency class that monitors a file or directory for changes.
Public method CacheDependency(String[]) Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories) for changes.
Public method CacheDependency(String, DateTime) Initializes a new instance of the CacheDependency class that monitors a file or directory for changes.
Public method CacheDependency(String[], DateTime) Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories) for changes and specifies a time when change monitoring begins.
Public method CacheDependency(String[], String[]) Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories), an array of cache keys, or both for changes.
Public method CacheDependency(String[], String[], DateTime) Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories), an array of cache keys, or both for changes.
Public method CacheDependency(String[], String[], CacheDependency) Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories), an array of cache keys, or both for changes. It also makes itself dependent upon a separate instance of the CacheDependency class.
Public method CacheDependency(String[], String[], CacheDependency, DateTime) Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories), an array of cache keys, or both for changes. It also makes itself dependent upon another instance of the CacheDependency class and a time when the change monitoring begins.
Top
  Name Description
Public property HasChanged Gets a value indicating whether the CacheDependency object has changed.
Public property UtcLastModified Gets the time when the dependency was last changed.
Top
  Name Description
Protected method DependencyDispose Releases the resources used by the CacheDependency class and any classes that derive from CacheDependency.
Public method Dispose Releases the resources used by the CacheDependency object.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method FinishInit Completes initialization of the CacheDependency object.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetUniqueID Retrieves a unique identifier for a CacheDependency object.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method NotifyDependencyChanged Notifies the base CacheDependency object that the dependency represented by a derived CacheDependency class has changed.
Protected method SetUtcLastModified Marks the time when a dependency last changed.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

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

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.
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.");


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ