在文件夹及其子文件夹上创建缓存依赖项

时间:2009-02-12 15:56:01

标签: asp.net caching asp.net-2.0 dependencies

在ASP.NET中我想在缓存中存储一​​个对象,该对象依赖于特定文件夹及其子文件夹中的所有文件。只是添加依赖于根文件夹的对象不起作用。除了在所有文件上创建一系列依赖项之外,还有任何合理的方法吗?

1 个答案:

答案 0 :(得分:7)

我相信你可以推出自己的缓存依赖关系并使用FileSystemMonitor来监控文件系统的变化。

更新:以下示例代码

public class FolderCacheDependency : CacheDependency
{
    public FolderCacheDependency(string dirName)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(dirName);
        watcher.Changed += new FileSystemEventHandler(watcher_Changed);
        watcher.Deleted += new FileSystemEventHandler(watcher_Changed);
        watcher.Created += new FileSystemEventHandler(watcher_Changed);
        watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
    }

    void watcher_Renamed(object sender, RenamedEventArgs e)
    {
        this.NotifyDependencyChanged(this, e);
    }

    void watcher_Changed(object sender, FileSystemEventArgs e)
    {
        this.NotifyDependencyChanged(this, e);
    }
}