CacheDependency Constructor (String[], String[])

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

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

public CacheDependency(
	string[] filenames,
	string[] cachekeys


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.

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.

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.

The following code fragment demonstrates how to insert an item into your application's Cache with a dependency on a key to another item placed in the cache. Since this method uses array syntax, you must define the number of keys on which the item you are adding to the Cache is dependent.

public void CreateDependency(Object sender, EventArgs e) {
    // Create a cache entry.
    Cache["key1"] = "Value 1";

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

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


.NET Framework

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