CacheDependency Constructor (String, DateTime)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of the CacheDependency class that monitors a file or directory for changes.

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

public CacheDependency(
	string filename,
	DateTime start
)

Parameters

filename
Type: System.String

The path to a file or directory that the cached object is dependent upon. When this resource changes, the cached object becomes obsolete and is removed from the cache.

start
Type: System.DateTime

The time against which to check the last modified date of the directory or file.

If the directory or file specified in the filename parameter is not found in the file system, it will be treated as missing. If the directory or file is missing when the object with the dependency is added to the Cache, the cached object will be removed from the Cache when the directory or file is created.

For example, assume that you add an object to the Cache with a dependency on the following file path: c:\stocks\xyz.dat. If that file is not found when the CacheDependency object is created, but is created later, the cached object is removed upon the creation of the xyz.dat file.

System_CAPS_noteNote

Change tracking begins immediately and is not directly based on the start parameter. Use the start parameter to pass a date and time in the past against which you want to check the last modified date of the directory or file passed in the filename parameter. If the last modified date is later than the date and time set passed in the start parameter, the cached item is removed from the Cache.

The following code example uses this constructor to instantiate a CacheDependency object, then inserts an item into the Cache with that dependency. 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
Available since 1.1
Return to top
Show: