Create a cache dependency on the folder and its subfolder

In ASP.NET, I would like to save the object in a cache, which has a dependency on all files in a specific folder and its subfolders. Just adding an object depending on the root folder does not work. Is there any reasonable way to do this other than chaining dependencies in all files?

+3
source share
1 answer

I believe that you can flip your own cache dependency and use FileSystemMonitor to monitor file system 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);
    }
}
+7
source

Source: https://habr.com/ru/post/1703112/


All Articles