HostFileChangeMonitor 类
定义
监视目录和文件路径,并通知缓存被监视项的更改情况。Monitors directories and file paths and notifies the cache of changes to the monitored items. 此类不能被继承。This class cannot be inherited.
public ref class HostFileChangeMonitor sealed : System::Runtime::Caching::FileChangeMonitor
public sealed class HostFileChangeMonitor : System.Runtime.Caching.FileChangeMonitor
type HostFileChangeMonitor = class
inherit FileChangeMonitor
Public NotInheritable Class HostFileChangeMonitor
Inherits FileChangeMonitor
- 继承
示例
下面的示例创建一个缓存项,该缓存项使用 HostFileChangeMonitor 对象监视源数据的状态, (是文件系统上的文件) 。The following example creates a cache item that uses a HostFileChangeMonitor object to monitor the state of the source data (which is a file) on the file system. 缓存项是使用对象定义的, CacheItemPolicy 用于提供缓存项的逐出和过期详细信息。The cache entry is defined using a CacheItemPolicy object to provide eviction and expiration details for the cache entry.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Caching;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click1(object sender, EventArgs e)
{
ObjectCache cache = MemoryCache.Default;
string fileContents = cache["filecontents"] as string;
if (fileContents == null)
{
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration =
DateTimeOffset.Now.AddSeconds(10.0);
List<string> filePaths = new List<string>();
string cachedFilePath = Server.MapPath("~") +
"\\cacheText.txt";
filePaths.Add(cachedFilePath);
policy.ChangeMonitors.Add(new
HostFileChangeMonitor(filePaths));
// Fetch the file contents.
fileContents = File.ReadAllText(cachedFilePath) + "\n"
+ DateTime.Now.ToString();
cache.Set("filecontents", fileContents, policy);
}
Label1.Text = fileContents;
}
}
Imports System.Runtime.Caching
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim cache As ObjectCache = MemoryCache.Default
Dim fileContents As String = TryCast(cache("filecontents"), _
String)
If fileContents Is Nothing Then
Dim policy As New CacheItemPolicy()
policy.AbsoluteExpiration = _
DateTimeOffset.Now.AddSeconds(10.0)
Dim filePaths As New List(Of String)()
Dim cachedFilePath As String = Server.MapPath("~") & _
"\cacheText.txt"
filePaths.Add(cachedFilePath)
policy.ChangeMonitors.Add(New _
HostFileChangeMonitor(filePaths))
' Fetch the file contents.
fileContents = File.ReadAllText(cachedFilePath) & _
vbCrLf & DateTime.Now.ToString()
cache.Set("filecontents", fileContents, policy)
End If
Label1.Text = fileContents
End Sub
End Class
注解
HostFileChangeMonitor类是类型的具体实现 FileChangeMonitor 。The HostFileChangeMonitor class is a concrete implementation of the FileChangeMonitor type. 此类是密封的,因此不能对其进行扩展。This class is sealed, therefore it cannot be extended. 如果要使用现有缓存实现并监视文件和目录的更改,则此类非常有用。This class is useful if you want to use an existing cache implementation and to monitor files and directories for changes.
对于每个指定的文件或目录路径, HostFileChangeMonitor 如果发生以下任何更改,则类会触发更改通知:For each specified file or directory path, the HostFileChangeMonitor class triggers a change notification if any of the following changes occur:
监视的文件或目录的名称更改。The name of the monitored file or directory changes.
创建监视器时,指定的文件或目录不存在,但以后创建。The specified file or directory did not exist at the time the monitor was created, but was created later. 换句话说,文件或目录是在监视项的作用域中创建的。In other words, a file or directory was created in the scope of the monitored items.
已更改的监视文件的大小。The size of a monitored file changed.
已更改监视文件的内容,或已更改监视目录的内容。The contents of a monitored file changed, or the contents of a monitored directory changed.
已更改文件或目录 (ACL) 的访问控制列表。The access control list (ACL) of the file or directory was changed.
所监视的文件或目录已被删除。The monitored file or directory was deleted.
如果同时对被监视的文件或目录发生了太多的更改,则 HostFileChangeMonitor 实例可能会无法跟踪特定更改。If too many changes occur for the monitored file or directory at the same time, the HostFileChangeMonitor instance can lose track of specific changes. 在此方案中, HostFileChangeMonitor 类会触发更改通知。In this scenario, the HostFileChangeMonitor class triggers a change notification. 当实例监视某个目录时,更有可能出现这种情况 HostFileChangeMonitor ,而在较短的时间内在目录结构的作用域中发生了许多更改。This scenario is more likely to occur when the HostFileChangeMonitor instance monitors a directory, and many changes occur in the scope of the directory structure in a short period of time.
由于类的用途 HostFileChangeMonitor 只是为了表明已在一组受监视的文件和目录之间更改了某些内容,因此不会将有关特定更改的详细信息视为不会被视为重要。Because the purpose of the HostFileChangeMonitor class is only to signal that something has changed among the set of monitored files and directories, it is not considered important that details about a specific change are not captured. 类的用途 HostFileChangeMonitor 是提供状态已更改的通知,以便可以逐出) 缓存条目 (或条目。The purpose of the HostFileChangeMonitor class is to provide notification that state changed so that a cache entry (or entries) can be evicted. 因为 HostFileChangeMonitor 类不会确切地指示所更改的内容,所以内部更改跟踪溢出无关。Because the HostFileChangeMonitor class does not indicate exactly what changed, internal-change tracking overflow is irrelevant.
为实例提供路径时 HostFileChangeMonitor ,目录和文件路径必须是目录或文件的完整路径。When you supply paths to a HostFileChangeMonitor instance, the directory and file paths must be full paths to the directory or file. 不允许在路径中使用相对路径和通配符。Relative paths and wildcard characters in paths are not allowed.
当 HostFileChangeMonitor 类在 ASP.NET 应用程序中使用时,用于访问被监视项的 Windows 标识是 ASP.NET 应用程序的应用程序标识。When the HostFileChangeMonitor class is used in an ASP.NET application, the Windows identity that is used for access to monitored items is the application identity for the ASP.NET application. 换句话说,应用程序标识将是以下各项之一:In other words, the application identity will be one of the following:
进程标识。The process identity.
配置的应用程序标识。The configured application identity.
UNC 凭据(如果应用程序从 UNC 共享运行)。The UNC credential if the application is running from a UNC share.
在 HostFileChangeMonitor non-ASP.NET 应用程序中使用类时,类在 FileSystemWatcher 内部使用,以监视文件。When the HostFileChangeMonitor class is used in a non-ASP.NET application, the FileSystemWatcher class is used internally to monitor files. 因此, (ACL) 的任何访问控制列表应用于受监视的文件或目录将应用于当前线程的 Windows 标识。As a result, whatever access control list (ACL) applies to a monitored file or directory is applied to the Windows identity of the current thread.
备注
调用方必须具有适当级别的代码访问安全性 (CA) 权限,并且必须对所有监视的目录和路径具有 NTFS 权限。Callers must have the appropriate level of code access security (CAS) permissions and must have NTFS permissions to all monitored directories and paths.
构造函数
| HostFileChangeMonitor(IList<String>) |
初始化 HostFileChangeMonitor 类的新实例。Initializes a new instance of the HostFileChangeMonitor class. |
属性
| FilePaths |
获取已传递给 HostFileChangeMonitor(IList<String>) 构造函数的目录和文件路径的集合。Gets the collection of directories and file paths that was passed to the HostFileChangeMonitor(IList<String>) constructor. |
| HasChanged |
获取一个值,该值指示由 ChangeMonitor 类监视的状态已发生更改。Gets a value that indicates that the state that is monitored by the ChangeMonitor class has changed. (继承自 ChangeMonitor) |
| IsDisposed |
获取一个值,该值指示释放 ChangeMonitor 类的派生实例。Gets a value that indicates that the derived instance of a ChangeMonitor class is disposed. (继承自 ChangeMonitor) |
| LastModified |
获取一个只读值,该值指示所监视文件或路径的上次写入时间。Gets a read-only value that indicates the last write time of a monitored file or path. |
| UniqueId |
获取基于被监视的目录和文件路径集的 HostFileChangeMonitor 实例的标识符。Gets an identifier for the HostFileChangeMonitor instance that is based on the set of monitored directories and file paths. |
方法
| Dispose() |
释放由 ChangeMonitor 类的当前实例占用的所有资源。Releases all resources that are used by the current instance of the ChangeMonitor class. (继承自 ChangeMonitor) |
| Dispose(Boolean) |
释放所有托管资源和非托管资源,以及对 ChangeMonitor 实例的任何引用。Releases all managed and unmanaged resources and any references to the ChangeMonitor instance. 此重载必须由派生的更改监视器类实现。This overload must be implemented by derived change-monitor classes. (继承自 ChangeMonitor) |
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| InitializationComplete() |
从派生类的构造函数进行调用以指示初始化已完成。Called from the constructor of derived classes to indicate that initialization is finished. (继承自 ChangeMonitor) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| NotifyOnChanged(OnChangedCallback) |
由缓存实施者调用来注册回调,并在某个依赖项已更改时通过 ObjectCache 委托通知 OnChangedCallback 实例。Called by Cache implementers to register a callback and notify an ObjectCache instance through the OnChangedCallback delegate when a dependency has changed. (继承自 ChangeMonitor) |
| OnChanged(Object) |
由派生类调用以在某个依赖项发生更改时引发事件。Called by derived classes to raise the event when a dependency changes. (继承自 ChangeMonitor) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |