简体   繁体   中英

how to use FileSystemWatcher to change cache data?

my application read DLL data from cache. but if any developer change DLLs DllCaching must change. So i have been using FileSystemWatcher to detect any changes on DLLs .

My systen Watcher mechanizm below: this project is in asp.net


    public void CreateFileWatcher(string path)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = path;

            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            watcher.IncludeSubdirectories = true;
            watcher.Filter = "*.dll";

            // Add event handlers.
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnChanged);

            // Begin watching.
            watcher.EnableRaisingEvents = true;
        }

   private static void OnChanged(object source, FileSystemEventArgs e)
        {

             //FillCache
}

Button1_Click
{
CreateFileWatcher(@"C:/data")

// like that:
    myarray = CachData
}

How to make it? how to load dll(again loading) when Dlls changes.

It is a hurdle on how to clear cache if you are using cache in your application and it becomes tough when you implement a custom logic to clear cache. If you are using EnterpriseLibrary cache, we can have a dependency while adding items to the cache. Any change to this file will clear the cache.

Below is the sample code. The same overload is available for asp.net cache as well.

FileDependency cacheFileDependency = new FileDependency("\\mynetworkpath\abc.txt");
cacheMgr.Add(cacheName, cacheValueList, 
             Microsoft.Practices.EnterpriseLibrary.Caching.CacheItemPriority.Normal,
             null, cacheFileDependency);

You do not understand the way ASP.NET works, i suppose. Here is my vision, hope it will helps you to understand the problem and find an appropriate solution.

I you instancing a FileSystemWatcher in a servers-side controls, that does not particularly means single-instance usage, that only means that that particular client accessed you web form or what will be granted (not really, actually) at least one thread. That is also means that IIS thread can be in less counts than it is required by the clients.

Addilitionally, that also means that you can not use an Singleton pattern effectively, as well as Session storage or cookies (it will break the law in case of web-farm async calls).

The only really working option is to implement a web service, and accumulate changes, deletes or additions to a specified server path (not a physical path, but rather a IIS (or any web server, fe mono) path ie \\etc\\bin\\dlls\\, which can be effectively translated to a physical server path using http utils BCL classes for the web), and to obtain required information periodically.

If you are willing to obtain such an information directly that will not be possible, because always it will be a gaps, between monitoring instances, where this folder will be in uncontrolled state, fe between one client thread is shutting down, and just before next client connects to a server, for example when user uploads a big file on a page, and then calls F5 or refreshes the page, but page gone to the expired state (expired state means, that meaningful state of the page for the client is lost, possibly, client consumer thread on a server is finished its work, or went to next client in a queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM