您应该能够使用
ChangeToken
实现类似于CacheDependency的功能。您必须使用其中一个文件提供程序才能获取的实例
IChangeToken
. 然后可以将此令牌用作缓存的参数。
非常基本的示例:
IMemoryCache cache = GetCache();
string fileContent = GetFileContent("sample.txt");
// instance of file provider should be ideally taken from IOC container
var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
IChangeToken token = _fileProvider.Watch("sample.txt");
// file will be removed from cache only if it is changed
var cacheEntryOptions = new MemoryCacheEntryOptions()
.AddExpirationToken(changeToken);
// put file content into cache
cache.Set("sample.txt", fileContent, cacheEntryOptions);
详细说明请参见
microsoft documentation for change tokens
.