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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -