c# - ASP.NET Caching, limiting the number of entries in cache -
i'm trying implement data caching web app in asp.net, class , i've been asked limit number of entries in objectcache, not memory size number of entries itself. quite easy since can call objectcache.count, when cache grows beyond established limit (5, testing) can't figure out how remove oldest element stored since it's alphabetically sorted.
this being implemented in service, @ data access layer can't use additional structure queue keep track of insertions in cache.
what can do? there way filter or older element in cache?
here's method code
public list<eventsummary> findevents(string keywords, long categoryid, int start, int count) { string querykey = "findevent-" + start + ":" + count + "-" + keywords.trim() + "-" + categoryid; objectcache cache = memorycache.default; list<eventsummary> val = (list<eventsummary>)cache.get(querykey); if (val != null) return val; category evncategory = categorydao.find(categoryid); list<event> fullresult = eventdao.findbyeventcategoryandkeyword(evncategory, keywords, start, count); list<eventsummary> summaryresult = new list<eventsummary>(); foreach (event evento in fullresult) { summaryresult.add(new eventsummary(evento.evnid, evento.evnname, evento.category, evento.evndate)); } if (cache.count() >= maxcachesize) { //what should here? } cache.add(querykey, summaryresult, datetime.now.adddays(cachedays)); return summaryresult; }
as mentioned in comments, trim
method memorycache
has lru (least used) policy, behavior looking here. unfortunately, method not based on absolute number of objects remove cache, rather on percentage, int
parameter. means that, if try hack way around , pass 1 / cache.count()
percentage, have no control on how many objects have been removed cache, not ideal scenario.
another way go diy approach , not use .net caching utilities since, in our case, not seem natively fit needs. i'm thinking of along lines of sorteddictionary timecode of cache objects key , list of cache objects inserted cache @ given timecode values. and, imo, not daring exercice try , reproduce .net cache behavior using, additionnal benefit of directly controlling removal policy yourself.
Comments
Post a Comment