Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a cache dependancy on a folder and its sub-folder

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?

like image 270
AnthonyWJones Avatar asked Jun 13 '26 12:06

AnthonyWJones


1 Answers

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);
    }
}
like image 163
Ramesh Avatar answered Jun 16 '26 08:06

Ramesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!