【发布时间】:2013-05-15 05:26:03
【问题描述】:
这是我的代码:
public class ConfigCache
{
private static volatile ObjectCache _cache = MemoryCache.Default;
private const string KeyModule = "MODULE_XDOC_KEY";
private static string _settingFile;
public ConfigCache(string file)
{
_settingFile = file;
}
public XDocument Get()
{
var doc = _cache[KeyModule] as XDocument;
if (doc == null)
{
doc = XDocument.Load(_settingFile);
var policy = new CacheItemPolicy();
var filePaths = new List<string> {_settingFile};
policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
var callback = new CacheEntryRemovedCallback(this.MyCachedItemRemovedCallback);
policy.RemovedCallback = callback;
_cache.Set(KeyModule, doc, policy);
}
return _cache[KeyModule] as XDocument;
}
private void MyCachedItemRemovedCallback(CacheEntryRemovedArguments arguments)
{
// Log these values from arguments list
}
}
当第一次遇到 _cache.Set() 时,它工作正常:
- _cache.Set() 效果很好,它将 xdoc 添加到缓存中。
但几分钟后(1 或 2 分钟),缓存将不再起作用:
- _cache.Set() 不会向缓存中插入任何内容
- _cache.Set() 不报任何错误。
- 从未触发回调 MyCachedItemRemovedCallback。
有人遇到了同样的问题: MemoryCache always returns "null" after first expiration
但似乎还没有解决。有人对此有任何想法吗?
【问题讨论】:
-
能解释一下宿主进程吗?它是作为托管在 IIS 中的 Web 应用程序运行,还是作为 Windows 服务运行?
-
宿主进程是IIS 7中的web应用,操作系统是win 7
标签: c# memorycache