c# - MemoryCache does not work after several minutes -
here code:
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] 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] xdocument; } private void mycacheditemremovedcallback(cacheentryremovedarguments arguments) { // log these values arguments list } }
when run _cache.set() first time, works fine:
- _cache.set() works well, add xdoc cache.
but after several minutes(1 or 2 minutes), cache not work anymore:
- _cache.set() not insert cache
- _cache.set() not report error.
- the callback mycacheditemremovedcallback never triggered.
someone met same issue: memorycache returns "null" after first expiration
but seems not resolved yet. have idea on this?
your problem might cache being disposed. result in memorycache
silently returning null
rather throwing exception. first search code sure not disposing it. if sure aren't disposing it, try breaking on appdomain.unhandledexception
event. memorycache
subscribes event (see this answer , disposes itself. if handling unhandledexception
event in app global error handler, problem.
if issue, simple workaround handle event , recreate new instance of cache. (see answer here)
Comments
Post a Comment