CacheDependency 类

定义

在存储于 ASP.NET 应用程序的 Cache 对象中的项与文件、缓存键、文件或缓存键的数组,或另一个 CacheDependency 对象之间建立依赖关系。Establishes a dependency relationship between an item stored in an ASP.NET application's Cache object and a file, cache key, an array of either, or another CacheDependency object. CacheDependency 类监视依赖关系,以便在任何这些对象发生更改时,自动删除缓存的项。The CacheDependency class monitors the dependency relationships so that when any of them changes, the cached item will be automatically removed.

public ref class CacheDependency : IDisposable
public class CacheDependency : IDisposable
type CacheDependency = class
    interface IDisposable
Public Class CacheDependency
Implements IDisposable
继承
CacheDependency
派生
实现

示例

下面的代码示例演示如何使用HasChanged属性来确定自上CacheDependency一次请求Cache后是否发生了更改。The following code example demonstrates using the HasChanged property to determine whether a CacheDependency changed since the previous request for an item in the Cache. 参数中传递DateTime.Nowdt start值设置为。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 

注解

如果将项添加到依赖于其他对象(如文件或文件数组)的缓存,则当对象发生更改时,将自动从缓存中删除该依赖项。If you add an item to the cache that is dependent on another object, such as a file or array of files, the dependent item is automatically removed from the cache when the object changes. 例如,假设您创建了一个DataSet基于 XML 文件中的数据的对象。For example, suppose you create a DataSet object based on data in an XML file. 您可以向缓存DataSet中添加一个CacheDependency对象,该对象使DataSet依赖于 XML 文件。You can add the DataSet to the cache with a CacheDependency object that makes the DataSet dependent on the XML file. 如果 XML 文件发生更改, DataSet则将从缓存中删除。If the XML file changes, the DataSet is removed from the cache.

可以通过AddCache.Insert方法将具有依赖项的项添加到应用程序的缓存中。You can add items with dependencies to your application's cache with the Add and Cache.Insert methods. 不能使用Item[String]属性将项添加到具有依赖项的缓存中。You cannot use the Item[String] property to add items to the cache with dependencies.

若要设置依赖项,请创建CacheDependency类的一个实例,指定该项依赖的文件、键或目录,然后将该依赖项传递Add给或Cache.Insert方法。To set up a dependency, you create an instance of the CacheDependency class, specifying the files, keys, or directories on which the item is dependent, and then pass the dependency to the Add or Cache.Insert method. CacheDependency实例可以表示一个文件或目录、一个文件或目录的数组,也可以表示一个文件或目录的数组以及一个缓存键数组(这些项表示存储Cache在对象中的其他项)。The CacheDependency instance can represent a single file or directory, an array of files or directories, or an array of files or directories along with an array of cache keys (these represent other items stored in the Cache object).

构造函数

CacheDependency()

初始化 CacheDependency 类的新实例。Initializes a new instance of the CacheDependency class.

CacheDependency(String)

初始化 CacheDependency 类的新实例,它监视文件或目录的更改情况。Initializes a new instance of the CacheDependency class that monitors a file or directory for changes.

CacheDependency(String, DateTime)

初始化 CacheDependency 类的新实例,它监视文件或目录的更改情况。Initializes a new instance of the CacheDependency class that monitors a file or directory for changes.

CacheDependency(String[])

初始化 CacheDependency 类的新实例,它监视一组(到文件或目录的)路径的更改情况。Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories) for changes.

CacheDependency(String[], DateTime)

初始化 CacheDependency 类的新实例,它监视一组(到文件或目录的)路径的更改情况并指定更改监视开始的时间。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[])

初始化 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.

CacheDependency(String[], String[], CacheDependency)

初始化 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. 它还使自己依赖于 CacheDependency 类的一个单独的实例。It also makes itself dependent upon a separate instance of the CacheDependency class.

CacheDependency(String[], String[], CacheDependency, DateTime)

初始化 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. 它还使自己依赖于 CacheDependency 类的另一个实例以及更改监视开始的时间。It also makes itself dependent upon another instance of the CacheDependency class and a time when the change monitoring begins.

CacheDependency(String[], String[], DateTime)

初始化 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.

属性

HasChanged

获取一个值,该值指示 CacheDependency 对象是否已更改。Gets a value indicating whether the CacheDependency object has changed.

UtcLastModified

获取依赖项的上次更改时间。Gets the time when the dependency was last changed.

方法

DependencyDispose()

释放由 CacheDependency 类和从 CacheDependency 派生的所有类使用的资源。Releases the resources used by the CacheDependency class and any classes that derive from CacheDependency.

Dispose()

释放由 CacheDependency 对象使用的资源。Releases the resources used by the CacheDependency object.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FinishInit()

完成 CacheDependency 对象的初始化。Completes initialization of the CacheDependency object.

GetFileDependencies()

获取文件依赖项。Gets the file dependencies.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
GetUniqueID()

检索 CacheDependency 对象的唯一标识符。Retrieves a unique identifier for a CacheDependency object.

ItemRemoved()

删除监视的缓存项时调用。Called when a monitored cache entry is removed.

KeepDependenciesAlive()

更新依赖于此项的每个缓存项的上次访问时间。Updates the last access time of every cache item that depends on this item.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
NotifyDependencyChanged(Object, EventArgs)

通知 CacheDependency 基对象由派生的 CacheDependency 类表示的依赖项已更改。Notifies the base CacheDependency object that the dependency represented by a derived CacheDependency class has changed.

SetCacheDependencyChanged(Action<Object,EventArgs>)

添加 Action 方法,处理通知感兴趣的参与方有关此依赖项的更改。Adds an Action method to handle notifying interested party in changes to this dependency.

SetUtcLastModified(DateTime)

标记依赖项的上次更改时间。Marks the time when a dependency last changed.

TakeOwnership()

允许第一个用户声明对此依赖项的独占拥有权。Allows the first user to declare exclusive ownership of this dependency.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅