【发布时间】:2021-09-13 16:26:12
【问题描述】:
我正在尝试使用 .NET Core 5.0 缓存 XML 文件。
我正在改编此页面中的示例 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/change-tokens?view=aspnetcore-5.0
我有以下代码,成功缓存文件(不是每次都从磁盘加载),但是当文件更改时,文件内容不会重新缓存。
public XmlDocument loadXML(string strFileName) {
XmlDocument xml;
// Try to obtain the file contents from the cache.
if (_cache.TryGetValue(strFileName, out xml)) {
return xml;
}
xml = this.createNewDocument();
xml.Load(strFileName);
if (xml != null) {
var changeToken = _fileProvider.Watch(strFileName);
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(5))
.AddExpirationToken(changeToken);
// Put the file contents into the cache.
_cache.Set(strFileName, xml, cacheEntryOptions);
}
return xml;
}
}
【问题讨论】:
标签: asp.net-core