In ASP.NET I would like to store an object in the cache which has a dependancy on all the files in specific folder and its sub-folders. Just adding the object with a dependancy on the root folder doesn't work. Is there in any reasonable way to do this other than creating a chain of dependancies on all the files?
I believe you can roll your own cache dependency and use FileSystemMonitor to monitor the filesystem changes.
Update: Sample code below
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With