调用回调函数后,ObjectCache不会更新

时间:2014-06-16 13:37:43

标签: c# asp.net global-asax objectcache

上下文:我需要一个类来从文本文件中将一些关键字放入缓存中。我在该文件上放置了一个监听器,因此当它被修改时,缓存会自行更新。

ASP .NET MVC 3 - Framework 4.5

的Global.asax:

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

BigBrother.cs课程:

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);
}

所以常见的工作流程是: 1.引发Application_Start,BigBrother将关键字放入缓存中并使用回调函数(效果很好) 2.收听文件“keywords.txt” 3.编辑文件“keywords.txt”时,刷新缓存(实际上,调用ReInitiateCaching,但我的缓存项“surveillanceKeywords”返回null)

整个过程都在我的本地环境中工作,但它无法在服务器上运行。我错过了什么吗?

谢谢。

0 个答案:

没有答案