CacheDependency Constructors

Definition

Initializes a new instance of the CacheDependency class.

Overloads

CacheDependency()

Initializes a new instance of the CacheDependency class.

CacheDependency(String)

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

CacheDependency(String[])

Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories) for changes.

CacheDependency(String, DateTime)

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

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.

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.

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.

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.

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.

CacheDependency()

Initializes a new instance of the CacheDependency class.

protected:
 CacheDependency();
protected CacheDependency ();
Protected Sub New ()

Examples

The following code example is a class that implements this version of the CacheDependency constructor as all classes that derive from CacheDependency are required to.

' Declare the class.
Public Class CustomCacheDependency 
   Inherits CacheDependency

     ' Constructor with no arguments 
     ' provided by CacheDependency class.
     Public Sub New()
     End Sub
   
     ' Declare a Boolean field named disposedValue.
     ' This will be used by Disposed property.
     Private disposedValue As Boolean                
     
     ' Create accessors for the Disposed property.
     Public Property Disposed As Boolean
       Get
           Return disposedValue
       End Get
       Set (ByVal value As Boolean)
           disposedValue = value
       End Set
     End Property
     
     ' Create a public method that sets the latest
     ' changed time of the CustomCacheDependency
     ' and notifies the underlying CacheDependency that the 
     ' dependency has changed, even though the HasChanged
     ' property is false.
     Public Sub ResetDependency()
        If Me.HasChanged = False              
           SetUtcLastModified(DateTime.MinValue)
           NotifyDependencyChanged(Me, EventArgs.Empty)
        End If
     End Sub
     
     ' Overrides the DependencyDispose method to set the
     ' Disposed proerty to true. This method automatically
     ' notifies the underlying CacheDependency object to 
     ' release any resources associated with this class. 
     Protected Overrides Sub DependencyDispose()
        Disposed = True
     End Sub
     
     
 End Class

Remarks

If you want to derive your own class from CacheDependency, implement this version of the CacheDependency constructor.

See also

Applies to

CacheDependency(String)

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

public:
 CacheDependency(System::String ^ filename);
public CacheDependency (string filename);
new System.Web.Caching.CacheDependency : string -> System.Web.Caching.CacheDependency
Public Sub New (filename As String)

Parameters

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

Examples

The following code example creates an instance of the CacheDependency class with a dependency on an XML file. The Cache.Insert method then adds an item to the Cache that is dependent upon that file.

CacheDependency dep = new CacheDependency(Server.MapPath("isbn.xml"));
Cache.Insert("ISBNData", Source, dep);

Dim dep As New CacheDependency(Server.MapPath("isbn.xml"))
    Cache.Insert("ISBNData", Source, dep)

You can also use the following technique to add an item to the Cache with a dependency on a file.

// Make key1 dependent on a file.
CacheDependency dependency = new CacheDependency(Server.MapPath("isbn.xml"));

Cache.Insert("key1", "Value 1", dependency);
    ' Make key1 dependent on a file.
    Dim dependency as new CacheDependency(Server.MapPath("isbn.xml"))

    Cache.Insert("key1", "Value 1", dependency)
End If

Remarks

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.

See also

Applies to

CacheDependency(String[])

Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories) for changes.

public:
 CacheDependency(cli::array <System::String ^> ^ filenames);
public CacheDependency (string[] filenames);
new System.Web.Caching.CacheDependency : string[] -> System.Web.Caching.CacheDependency
Public Sub New (filenames As String())

Parameters

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

Examples

The following code example demonstrates how to use the Cache.Insert method to add an item to the Cache with dependencies on an array of files. You can also make the item dependent on an array of directories.

    // Make key1 dependent on several files.
    String[] files = new String[2];
    files[0] = Server.MapPath("isbn.xml");
    files[1] = Server.MapPath("customer.xml");
    CacheDependency dependency = new CacheDependency(files);

    Cache.Insert("key1", "Value 1", dependency);
}
    ' Make key1 dependent on several files.
    Dim files(2) as String
    files(0) = Server.MapPath("isbn.xml")
    files(1) = Server.MapPath("customer.xml")
    Dim dependency as new CacheDependency(files)

    Cache.Insert("key1", "Value 1", dependency)
End If

Remarks

If any of the files or directories in the array changes or is removed from the array, the cached item becomes obsolete and is removed from the application's Cache 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.

Note

When you create the array that contains the file dependencies, you must define the number of files that the item you are adding to the Cache is dependent on.

See also

Applies to

CacheDependency(String, DateTime)

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

public:
 CacheDependency(System::String ^ filename, DateTime start);
public CacheDependency (string filename, DateTime start);
new System.Web.Caching.CacheDependency : string * DateTime -> System.Web.Caching.CacheDependency
Public Sub New (filename As String, start As DateTime)

Parameters

filename
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
DateTime

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

Examples

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.");
' 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.")
Else
   Response.Write("<p>The dependency has not changed.")
End If 

Remarks

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.

Note

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.

See also

Applies to

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:
 CacheDependency(cli::array <System::String ^> ^ filenames, DateTime start);
public CacheDependency (string[] filenames, DateTime start);
new System.Web.Caching.CacheDependency : string[] * DateTime -> System.Web.Caching.CacheDependency
Public Sub New (filenames As String(), start As DateTime)

Parameters

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

start
DateTime

The time against which to check the last modified date of the objects in the array.

Examples

The following code example creates a CacheDependency object that passes two XML files and a DateTime.Now value when the object is included as a parameter argument in an Cache.Insert method call.

// Create a DateTime object that determines
// when dependency monitoring begins.
DateTime dt = DateTime.Now;
    // Make key1 dependent on several files.
    String[] files = new String[2];
    files[0] = Server.MapPath("isbn.xml");
    files[1] = Server.MapPath("customer.xml");
    CacheDependency dep = new CacheDependency(files, dt);

    Cache.Insert("key1", "Value 1", dep);
}
    ' Create a DateTime object that determines
    '  when dependency monitoring begins.
    Dim dt As DateTime = DateTime.Now

    ' Make key1 dependent on several files.
    Dim files(2) as String
    files(0) = Server.MapPath("isbn.xml")
    files(1) = Server.MapPath("customer.xml")
    Dim dependency as new CacheDependency(files, dt)

    Cache.Insert("key1", "Value 1", dependency)
End If

Remarks

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.

Note

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 array passed in the filenames parameter. If the last modified date of any object in the array is later than the date and time set passed in the start parameter, the cached item is removed from the Cache.

See also

Applies to

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:
 CacheDependency(cli::array <System::String ^> ^ filenames, cli::array <System::String ^> ^ cachekeys);
public CacheDependency (string[] filenames, string[] cachekeys);
new System.Web.Caching.CacheDependency : string[] * string[] -> System.Web.Caching.CacheDependency
Public Sub New (filenames As String(), cachekeys As String())

Parameters

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

Examples

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

    DisplayValues();
}
Public Sub CreateDependency(sender As Object, e As EventArgs)
    ' Create a cache entry.
    Cache("key1") = "Value 1"

    ' Make key2 dependent on key1.
    Dim dependencyKey(0) As String
    dependencyKey(0) = "key1"
    Dim dependency As new CacheDependency(Nothing, dependencyKey)

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

    DisplayValues()
End Sub

Remarks

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.

However, the cachekeys dependency does not work the same way. If at least one value in the cachekeys parameter does not exist at the time of an insert, the insert fails. Note that there is no exception thrown for this scenario.

See also

Applies to

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:
 CacheDependency(cli::array <System::String ^> ^ filenames, cli::array <System::String ^> ^ cachekeys, DateTime start);
public CacheDependency (string[] filenames, string[] cachekeys, DateTime start);
new System.Web.Caching.CacheDependency : string[] * string[] * DateTime -> System.Web.Caching.CacheDependency
Public Sub New (filenames As String(), cachekeys As String(), start As DateTime)

Parameters

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

start
DateTime

The date and time against which to check the last modified date of the objects passed in the filenames and cachekeys arrays.

Examples

The following code example creates 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 of key1 as well. This CacheDependency constructor then instantiates a CacheDependency object that passes dependencyKey and the DateTime object as parameter arguments. The Cache.Insert method is called next, using the CacheDependency object as a parameter. This makes the object added to the cache using the Insert method dependent on the key1 key.

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 dependency = new CacheDependency(null, dependencyKey, dt);

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

    DisplayValues();
}
Public Sub CreateDependency(sender As Object, e As EventArgs)
' Create a DateTime object.
Dim dt as DateTime = DateTime.Now.AddSeconds(10)        

' Create a cache entry.
    Cache("key1") = "Value 1"

    ' Make key2 dependent on key1.
    Dim dependencyKey(0) As String
    dependencyKey(0) = "key1"
    Dim dependency As new CacheDependency(Nothing, dependencyKey, dt)

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

    DisplayValues()
End Sub

Remarks

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.

However, the cachekeys dependency does not work the same way. If at least one value in the cachekeys parameter does not exist at the time of an insert, the insert fails. Note that there is no exception thrown for this scenario.

Note

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

See also

Applies to

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:
 CacheDependency(cli::array <System::String ^> ^ filenames, cli::array <System::String ^> ^ cachekeys, System::Web::Caching::CacheDependency ^ dependency);
public CacheDependency (string[] filenames, string[] cachekeys, System.Web.Caching.CacheDependency dependency);
new System.Web.Caching.CacheDependency : string[] * string[] * System.Web.Caching.CacheDependency -> System.Web.Caching.CacheDependency
Public Sub New (filenames As String(), cachekeys As String(), dependency As CacheDependency)

Parameters

filenames
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
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
CacheDependency

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

Examples

The following code example creates a CreateDependency method. When this method is called, it uses the Cache.Item[] property 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 of key1 as well. The CacheDependency.CacheDependency constructor is used to create a CacheDependency object, dep1, which passes dependencyKey as a parameter argument. A second CacheDependency object, dep2, is created using this constructor, with dep1 passed as the third parameter argument. This second dependency is dependent on the first. 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 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.        
    CacheDependency dep2 = new CacheDependency(null, null, dep1);

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

    DisplayValues();
}
 Public Sub CreateDependency(sender As [Object], e As EventArgs)
   ' Create a cache entry.
   Cache("key1") = "Value 1"

   ' Make key2 dependent on key1 using double dependency.
   Dim dependencyKey(0) As [String]
   dependencyKey(0) = "key1"
   Dim dep1 As New CacheDependency(Nothing, dependencyKey)

   ' Make a second CacheDependency dependent on dep1.        
   Dim dep2 As New CacheDependency(Nothing, Nothing, dep1)

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

   DisplayValues()
End Sub 'CreateDependency

Remarks

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.

However, the cachekeys dependency does not work the same way. If at least one value in the cachekeys parameter does not exist at the time of an insert, the insert fails. Note that there is no exception thrown for this scenario.

Applies to

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.

public:
 CacheDependency(cli::array <System::String ^> ^ filenames, cli::array <System::String ^> ^ cachekeys, System::Web::Caching::CacheDependency ^ dependency, DateTime start);
public CacheDependency (string[] filenames, string[] cachekeys, System.Web.Caching.CacheDependency dependency, DateTime start);
new System.Web.Caching.CacheDependency : string[] * string[] * System.Web.Caching.CacheDependency * DateTime -> System.Web.Caching.CacheDependency
Public Sub New (filenames As String(), cachekeys As String(), dependency As CacheDependency, start As DateTime)

Parameters

filenames
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
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
CacheDependency

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

start
DateTime

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

Examples

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();
}
 Public Sub CreateDependency(sender As [Object], e As EventArgs)
   ' Create a DateTime object.
   Dim dt as DateTime = DateTime.Now.AddSeconds(10)        

   ' Create a cache entry.
   Cache("key1") = "Value 1"

   ' Make key2 dependent on key1 using double dependency.
   Dim dependencyKey(0) As [String]
   dependencyKey(0) = "key1"
   Dim dep1 As New CacheDependency(Nothing, dependencyKey)

   ' Make a second CacheDependency dependent on dep1
   ' and use dt to start change monitoring.        
   Dim dep2 As New CacheDependency(Nothing, Nothing, dep1, dt)

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

   DisplayValues()
End Sub 'CreateDependency

Remarks

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.

However, the cachekeys dependency does not work the same way. If at least one value in the cachekeys parameter does not exist at the time of an insert, the insert fails. Note that there is no exception thrown for this scenario.

Note

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.

See also

Applies to