ObjectCache not updating after calling the callback function

427 Views Asked by At

Context: I need a class to put some keywords in the cache from a text file. I put a listener on that file so when it's modified, the cache update itself.

ASP .NET MVC 3 -- Framework 4.5

Global.asax:

protected void Application_Start()
{
    // Caching keywords
    BigBrother.InitiateCaching(BigBrother.ReInitiateCaching);
}

BigBrother.cs class:

private static readonly Cache _cacheHandler = new Cache();

public static void InitiateCaching(CacheEntryRemovedCallback pCallBackFunction)
{
    string filePath = HostingEnvironment.MapPath("~/Configurations/Surveillance/keywords.txt");
    var fileContent = File.ReadAllText(filePath, Encoding.UTF8).Split(';');
    _cacheHandler.AddToMyCache("surveillanceKeywords", fileContent, CachePriority.Default, new List<string> { filePath }, pCallBackFunction);
}

public static void ReInitiateCaching(CacheEntryRemovedArguments arguments)
{
    InitiateCaching(ReInitiateCaching);
}

Cache.cs:

public void AddToMyCache(string cacheKeyName, object cacheItem, CachePriority cacheItemPriority, List<string> filePath,
        CacheEntryRemovedCallback pCallBackFunction)
{
        callback = pCallBackFunction;

        policy = new CacheItemPolicy
        {
            Priority = (cacheItemPriority == CachePriority.Default) ? CacheItemPriority.Default : CacheItemPriority.NotRemovable,
            AbsoluteExpiration = DateTimeOffset.Now.AddYears(1),
            RemovedCallback = callback
        };
        policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePath));
        cache.Set(cacheKeyName, cacheItem, policy);
}

So the common workflow is: 1. Application_Start is fired and BigBrother put keywords in the cache with the callback function (works very well) 2. Listen the file "keywords.txt" 3. When the file "keywords.txt" is edited, refresh cache (actually, ReInitiateCaching is called, but my cache item "surveillanceKeywords" return null)

The whole thing is working in my local environment, but it's not working on the server. Am I missing something?

Thank you.

0

There are 0 best solutions below