How to prevent HttpRuntime.Cache to remove items in ASP.NET 4.5? -


this follow-up this question, contains contradictory answers. interested in answer related more recent version of asp.net.

my application uses httpruntime.cache cache lists of models should never expire. loaded on application warmup , changed quite rarely, read quite often.

my code following:

private void reportremovedcallback(string key, object value, cacheitemremovedreason reason) {     if (!applicationpoolservice.isshuttingdown())     {         var str = $"removed cached item key {key} , count {(value idictionary)?.count}, reason {reason}";         loggingservice.log(loglevel.info, str);     } }  private idictionary<int, t> threadsafecacheaccessaction(action<idictionary<int, t>, bool> action = null) {     // refresh cache if necessary     var dict = httpruntime.cache[cachedictkey] idictionary<int, t>;     bool invalidated = false;     if (dict == null)     {         lock (cachelockobject)         {             // getting expiration times model attribute             var cacheinfo = typeof(t).getcustomattributes(typeof(useincachedrepositoryattribute), inherit: true).firstordefault() useincachedrepositoryattribute;             int absoluteexpiration = cacheinfo?.absoluteexpiration ?? constants.cache.indefiniteretention;             int slidingexpiration = cacheinfo?.slidingexpiration ?? constants.cache.noslidingexpiration;              dict = _modelrepository.allnotracking.tolist().where(item => item.pkid != 0).todictionary(item => item.pkid, item => item);             httpruntime.cache.insert(cachedictkey, dict, dependencies: null,                 absoluteexpiration: datetime.now.addminutes(absoluteexpiration),                 slidingexpiration: slidingexpiration <= 0 ? cache.noslidingexpiration : timespan.fromminutes(slidingexpiration),                 priority: cacheitempriority.notremovable,                 onremovecallback: reportremovedcallback);              invalidated = true;         }     } 

based on documentation provided here, have included following markup within web.config:

<caching>   <cache disableexpiration="true" disablememorycollection="true" /> </caching> 

however, time time, reportremovedcallback called item removal. feeling caching configuration web.config ignored (the documentation states outdated) , cacheitempriority.notremovable means "a high priority", not "never remove".

question: is there way convince httpruntime.cache never remove items? or should consider caching mechanism?

ok, have dug more , there no definitive answer, following configuration this old docs seems apply regardless of trials deny expiration:

the following default cache element not explicitly configured in machine configuration file or in root web.config file, default configuration returned application in .net framework version 2.0.

<cache disablememorycollection="false"    disableexpiration="false" privatebyteslimit="0"    percentagephysicalmemoryusedlimit="90"    privatebytespolltime="00:02:00" />  

so, if physical memory usage above 90%, evict cache items. since os tends use physical memory system cache (reported task manager), not unlikely sounds.

alternative

i took memorycache spin, since similar httpruntime.cache. provides similar functionality, lacks cacheentryupdatecallback (you can provide it, complains invalidargumentexception if different null).

now code following:

var dict = memorycache.default.get(cachedictkey) idictionary<int, t>;  if (dict == null) {     lock (cachelockobject)     {         // getting expiration times model attribute         var cacheinfo = typeof(t).getcustomattributes(typeof(useincachedrepositoryattribute), inherit: true).firstordefault() useincachedrepositoryattribute;         int absoluteexpiration = cacheinfo?.absoluteexpiration ?? constants.cache.indefiniteretention;         int slidingexpiration = cacheinfo?.slidingexpiration ?? constants.cache.noslidingexpiration;          dict = _modelrepository.allnotracking.tolist().where(item => item.pkid != 0).todictionary(item => item.pkid, item => item);          var cacheitempolicy = new cacheitempolicy         {             absoluteexpiration = datetime.now.addminutes(absoluteexpiration),             slidingexpiration = slidingexpiration <= 0 ? cache.noslidingexpiration : timespan.fromminutes(slidingexpiration),             priority = system.runtime.caching.cacheitempriority.notremovable,             // throws invalidargumentexception             // updatecallback = cacheentryupdatecallback         };         memorycache.default.add(cachedictkey, dict, cacheitempolicy);     } } 

after tests, there no removals , memory consumption of w3wp.exe raised expected.

more details provided within this answer.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -