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