首页 > 解决方案 > 为什么 Filedepency 上的 ExpireToken 不清除 CacheEntry?

问题描述

我使用 IMemoryCache 缓存 Razorpages 网站中的不同对象。我使用以下方法添加它们:

public void AddEntryToCache(string key, object obj)
{
    var fileProvider = new PhysicalFileProvider(@"c:\mycachefolder");
    var changeToken = fileProvider.Watch("cacheclearfile.txt");

    // entry should be cleared when txt-file changes
    MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
    options.AddExpirationToken(changeToken);

    // _cache is the injected IMemoryCache-Object
    _cache.Set(key, obj, options);
}

这个方法是我在 Startup 中添加的一个类的一部分:

services.AddSingleton<UtilCache>();

我添加了一个文件依赖。但是当我更改文件的内容时,缓存条目不会被清除并且仍然保留在缓存中。

我错过了什么?

标签: c#caching.net-corerazor-pages

解决方案


推荐阅读