Breyta

Deila með


HostFileChangeMonitor Class

Definition

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
Inheritance
HostFileChangeMonitor

Examples

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

Remarks

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.

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.

  • The access control list (ACL) of the file or directory was changed.

  • The monitored file or directory was deleted.

If too many changes occur for the monitored file or directory at the same time, the HostFileChangeMonitor instance can lose track of specific changes. In this scenario, the HostFileChangeMonitor class triggers a change notification. 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.

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. The purpose of the HostFileChangeMonitor class is to provide notification that state changed so that a cache entry (or entries) can be evicted. Because the HostFileChangeMonitor class does not indicate exactly what changed, internal-change tracking overflow is irrelevant.

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.

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.

  • The UNC credential if the application is running from a UNC share.

When the HostFileChangeMonitor class is used in a non-ASP.NET application, the FileSystemWatcher class is used internally to monitor files. 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.

Note

Callers must have the appropriate level of code access security (CAS) permissions and must have NTFS permissions to all monitored directories and paths.

Constructors

HostFileChangeMonitor(IList<String>)

Initializes a new instance of the HostFileChangeMonitor class.

Properties

FilePaths

Gets the collection of directories and file paths that was passed to the HostFileChangeMonitor(IList<String>) constructor.

HasChanged

Gets a value that indicates that the state that is monitored by the ChangeMonitor class has changed.

(Inherited from ChangeMonitor)
IsDisposed

Gets a value that indicates that the derived instance of a ChangeMonitor class is disposed.

(Inherited from ChangeMonitor)
LastModified

Gets a read-only value that indicates the last write time of a monitored file or path.

UniqueId

Gets an identifier for the HostFileChangeMonitor instance that is based on the set of monitored directories and file paths.

Methods

Dispose()

Releases all resources that are used by the current instance of the ChangeMonitor class.

(Inherited from ChangeMonitor)
Dispose(Boolean)

Releases all managed and unmanaged resources and any references to the ChangeMonitor instance. This overload must be implemented by derived change-monitor classes.

(Inherited from ChangeMonitor)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializationComplete()

Called from the constructor of derived classes to indicate that initialization is finished.

(Inherited from ChangeMonitor)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
NotifyOnChanged(OnChangedCallback)

Called by Cache implementers to register a callback and notify an ObjectCache instance through the OnChangedCallback delegate when a dependency has changed.

(Inherited from ChangeMonitor)
OnChanged(Object)

Called by derived classes to raise the event when a dependency changes.

(Inherited from ChangeMonitor)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to