1

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

ASP .NET MVC 3 -- 框架 4.5

全球.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);
}

缓存.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)

整个事情在我的本地环境中工作,但它不在服务器上工作。我错过了什么吗?

谢谢你。

4

0 回答 0