Export (0) Print
Expand All

CacheDependency Constructor (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.

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

public CacheDependency(
	string[] filenames,
	string[] cachekeys,
	CacheDependency dependency,
	DateTime start
)

Parameters

filenames
Type: System.String[]

An array of paths (to files or directories) that the cached object is dependent upon. When any of these resources changes, the cached object becomes obsolete and is removed from the cache.

cachekeys
Type: System.String[]

An array of cache keys that the new object monitors for changes. When any of these cache keys changes, the cached object associated with this dependency object becomes obsolete and is removed from the cache.

dependency
Type: System.Web.Caching.CacheDependency

Another instance of the CacheDependency class that this instance is dependent upon.

start
Type: System.DateTime

The time against which to check the last modified date of the objects in the arrays and the CacheDependency object.

Also, if any of the directories or files specified in the filenames parameter is not found in the file system, it is treated as missing. If a 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 that 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.

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 any of the objects passed in the filenames, cachekeys, or dependency parameters. If the last modified date for any of those objects is later than the date and time passed in the start parameter, the cached item is removed from the Cache.

The following code example shows a CreateDependency method. When this method is called, it creates a DateTime object and the Cache.Item property is used to add an item to the cache with a key parameter of key1 and a value of Value 1. An array of strings, dependencyKey, is then created with the value at its first (and only) index set to the value of key1 as well. The CacheDependency.CacheDependency constructor is then used to create a CacheDependency object, dep1, which passes dependencyKey as a parameter argument. A second CacheDependency object, dep2, is then created using this constructor, with dep1 and the DateTime object passed as parameter arguments. This second dependency is dependent on the first, and dependency monitoring will begin 10 seconds after the CreateDependency method is called. The Cache.Insert method is called next, using the second CacheDependency object as a parameter. If the first dependency changes in any way, the cached item will be invalidated.

public void CreateDependency(Object sender, EventArgs e) {
    // Create a DateTime object.
    DateTime dt = DateTime.Now.AddSeconds(10);

    // Create a cache entry.
    Cache["key1"] = "Value 1";

    // Make key2 dependent on key1.
    String[] dependencyKey = new String[1];
    dependencyKey[0] = "key1";
    CacheDependency dep1 = new CacheDependency(null, dependencyKey);

    // Make a second CacheDependency dependent on dep1.
    // and use dt to start change monitoring.        
    CacheDependency dep2 = new CacheDependency(null, null, dep1, dt);

    Cache.Insert("key2", "Value 2", dep2);

    DisplayValues();
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
Show:
© 2015 Microsoft