Using HostFileChangeMonitor in MemoryCache to synch Multiple asp.net web sites RRS feed

  • Question

  • User-1554502115 posted

    I am implementing asp.net web site which is going to be deployed as web farm. I am trying to incorporate “MemoryCache" to cache frequently used entities. Since every server will have its own In-memory cache, there will be an issue of keeping this cache in synch across all the web servers. To resolve this I have used  “HostFileChangeMonitor"

    Here is what I have implemented:

    1.Every item in “MemoryCache"  will have dependency on “HostFileChangeMonitor” monitor.

    2.This monitor will be created over a file shared on NAS which will be accessible to all the servers.

    3.Whenever I need to flush/remove the entry from the cache, I will update the file, which will be watched by the “HostFileChangeMonitor” monitor. Then  automatically the entry will be removed from the cache on all the servers.

    Code snippet:

    private CacheItemPolicy GetCachePolicy(CacheKey key)
                CacheItemPolicy policy = new CacheItemPolicy();
                HostFileChangeMonitor fileMonitor = new HostFileChangeMonitor(new List<string>() {key.CreateFilePath(_cacheName) });
                return policy;

    I have tested it against couple of web servers and it seems to be doing its job. someone please confirm following:

    1. Is this a safe approach to synch the cache across multiple servers?

    2. I have not created any event listening to “file change” even when the file is modified. The “HostFileChangeMonitor” monitor is doing that for me and removing the cache entry. Is this safe assumption?

    3. On production I will have around 5 instances of the cache. Each containing 1000 items. This means, I will have around 5000 files watched by couple of web servers. With keeping above data in mind, is there any issue from I/O perspective since around 5000  “HostFileChangeMonitor” monitors on each server will be created?

    Tuesday, August 30, 2016 11:24 AM

All replies