惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 飘渺峰

一致性Hash算法 .net Parallel并行使用注意事项 析构函数和Dispose方法的区别 查看SQLServer的最大连接数 Hash算法-CityHash算法 Hash算法 Sunday算法--C#版 KMP算法--C#版 BoyerMoore(BM)算法--C# [转]软件项目管理总体流程设计 全排列和组合算法 生活 负载均衡算法--C#版 结束进程的方法forceStopPackage 【转】OAUTH协议简介 SQL Server FOR XML PATH 语句的应用 nlog轻量级日志组件 反射加载程序集的几个方法的区别 windows 下TCP最大连接数
HostFileChangeMonitor
飘渺峰 · 2013-08-02 · via 博客园 - 飘渺峰

HostFileChangeMonitor 类是 FileChangeMonitor 类型的具体实现。 此类密封,因此无法扩展。 如果要使用现有缓存实现以及监视更改的文件和目录,此类非常有用。

对于每个指定的文件或目录路径,HostFileChangeMonitor 类在发生以下任何更改时触发更改通知:

  • 被监视文件或目录的的名称更改。

  • 指定的文件或目录在创建监视器时不存在,但后来被创建。 换句话说,在被监视项的范围内创建文件或目录。

  • 更改的被监视文件的大小。

  • 被监视文件的内容已更改,或被监视目录的内容已更改。

  • 文件或目录的访问控制列表 (ACL) 已更改。

  • 被监视文件或目录已被删除。

如果被监视的文件或目录同时发生了太多更改,则 HostFileChangeMonitor 实例可能失去特定更改的跟踪。 在此方案中,HostFileChangeMonitor 类触发更改通知。 HostFileChangeMonitor 实例监视某个目录,并且短期内在目录结构的范围内发生了许多更改时,很可能发生此情况。

由于 HostFileChangeMonitor 类的用途只是通知受监控的文件和目录中内容有更改,因此有关特定更改没有捕获到的详细信息并不被认为是重要的。 HostFileChangeMonitor 类的用途在于提供状态已更改的通知,以便逐出缓存项。 由于 HostFileChangeMonitor 类没有明确指明更改了什么,因此内部更改跟踪溢出不相关。

当您向 HostFileChangeMonitor 实例提供路径时,目录和文件路径必须是目录或文件的完整路径。 不允许使用相对路径以及在路径中使用通配符字符。

HostFileChangeMonitor 类用于 ASP.NET 应用程序时,用于访问被监视项的 Windows 标识将是 ASP.NET 应用程序的应用程序标识。 换句话说,应用程序标识将是以下项之一:

  • 进程标识。

  • 配置的应用程序标识。

  • 应用程序从 UNC 共享中运行时的 UNC 凭据。

HostFileChangeMonitor 类用于非 ASP.NET 应用程序时,在内部使用 FileSystemWatcher 类监视文件。 因此,将用于受监视文件或目录的任何访问控制列表 (ACL) 应用于当前线程的 Windows 标识。

举例如:

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