Search code examples
asp.netcachingfilesystemwatcher

FileSystemWatcher or ASP.NET Cache for multiple files


I'm working on an open source where I need to populate a dictionary from reading values from multiple xml files and whenever any of the xml file changes I have to update the dictionary. Since there are multiple files I thought of using FileSystemWatcher. Is this a good idea? or I've to go with ASP.NET cache with file dependency? I'm not sure how ASP.NET cache helps me in the case of multiple files.

Note: I've implemented a locking mechainsm while updating the dictionary so threading won't be an issue in the case of updating dictionary.


Solution

  • For creating dependencies for multiple files you can use the AggregateCacheDependency like this:

    CacheDependency[] depArray = new CacheDependency () 
    {
      new CacheDependency(Server.MapPath("foo.txt")),
      new CacheDependency(Server.MapPath("bar.txt"))
    };
    
    AggregateCacheDependency aggDep = new AggregateCacheDependency();
    aggDep.Add(depArray);
    
    Cache.Insert("key", value, aggDep);