ch
Creating a CacheDependency
The
dependencies parameter for the
Add and
Insert methods of the ASP.NET Cache object, shown in Table 2, is a reference to a CacheDependency instance that you create in your code. The CacheDependency class exposes several constructors, as shown in Table 3. These allow you to create a dependency that will invalidate the cached item in response to a range of events.
Table 3. The table lists the constructors for the CacheDependency class.
| Constructor
|
Details
|
| CacheDependency()
|
Creates a CacheDependency with no dependent objects.
|
| CacheDependency("file/folder-path")
|
Creates a CacheDependency that will invalidate the cache when the specified file or folder (a String value) changes.
|
| CacheDependency(array-of-file/folder-paths)
|
Creates a CacheDependency that will invalidate the cache when any of the specified files or folders (an array of String values) change.
|
| CacheDependency("file/folder-path",
DateTime)
|
Creates a CacheDependency that will invalidate the cache when the specified file or folder changes, or at the specified time.
|
| CacheDependency(array-of-file/folder-paths,
DateTime)
|
Creates a CacheDependency that will invalidate the cache when any of the specified files or folders change, or at the specified time.
|
| CacheDependency(array-of-file/folder-paths,
array-of-cache-keys)
|
Creates a CacheDependency that will invalidate the cache when any of the specified files or folders change, or any of the specified cache items (as an array of String values containing the cache keys) change.
|
| CacheDependency(array-of-file/folder-paths,
array-of-cache-keys, DateTime)
|
Creates a CacheDependency that will invalidate the cache when any of the specified files or folders change, or any of the specified cache items change, or at the specified time.
|
| CacheDependency(array-of-file/folder-paths,
array-of-cache-keys,
CacheDependency)
|
Creates a CacheDependency that will invalidate the cache when any of the specified files or folders change, or any of the specified cache items change, or the specified CacheDependency causes its data to be invalidated.
|
| CacheDependency(array-of-file/folder-paths,
array-of-cache-keys,
CacheDependency, DateTime)
|
Creates a CacheDependency that will invalidate the cache when any of the specified files or folders change, or any of the specified cache items change, or the specified CacheDependency causes it's data to be invalidated, or at the specified time.
|
| Author's Note: See the .NET Framework SDK topic "CacheDependency Members" in the "Reference | Class Library" section for more details. |
Chaining Dependencies
Using the constructors for the
CacheDependency class shown in Table 3, you can create chains of dependencies. This allows the invalidation of one dependency to force invalidation of the chained dependencies. For example:
' add an item that depends on the file myfile.xml,
' and that expires after 10 minutes
Cache.Insert("first-key", myFirstValue, _
New CacheDependency(Server.MapPath("myfile.xml")), _
DateTime.Now.AddMinutes(10), TimeSpan.Zero, _
CacheItemPriority.High, Nothing)
' create an array of file paths/names
Dim aFiles() As String
aFiles(0) = Server.MapPath("anotherfile.xml"),
aFiles(1) = Server.MapPath("astylesheet.xslt"),
Dim dep As New CacheDependency(aFiles, _
DateTime.Now.AddMinutes(25))
' create an array with one cache key name
Dim aDeps() As String = {"first-key"}
' create a SqlCacheDependency for the Northwind Orders table
Dim sqlDep As New SqlCacheDependency("nwind-cache", "Orders")
' now add an item to the cache that depends on the three previous dependencies
Cache.Insert("second-key", mySecondValue, _
New CacheDependency(aFiles, aDeps, sqlDep), _
DateTime.Now.AddMinutes(10), TimeSpan.Zero, _
CacheItemPriority.High, Nothing)