c# - Generic Object Cache -
i working on project plan on using redis persistent data storage, task @ hand, working on generic object cache. , huge fan of linq have started designing cache support this.
public concurrentbag<object> cache = new concurrentbag<object>(); public list<t> findby<t>(func<t, bool> predicate) t : class { var tmp = new list<t>(); foreach (var in cache) { try { t obj = t; if (obj != null) tmp.add(obj); } catch { } } return tmp.where(predicate).tolist(); }
i afraid when object cache grows large become inefficient. (i estimate 500k-1m objects)
i hoping possible use this
public concurrentbag<object> cache = new concurrentbag<object>(); public list<t> findby<t>(func<t, bool> predicate) t : class { return cache.where<t>(predicate).tolist(); }
hopefully not off-track here? suggestions welcome :)
since estimate lot of items in cache , operations on cache type specific, use multiple bags wrapped dictionary. speed finding subset of cache of type of interest , ideal if cache contained many minimal subsets of different types.
readonly idictionary<type, concurrentbag<object>> _cache = new concurrentdictionary<type, concurrentbag<object>>(); public list<t> findby<t>(func<t, bool> predicate) t : class { // check if items of type {t} exist in cache. concurrentbag<object> bag; if (_cache.trygetvalue(typeof (t), out bag)) { // cast, apply predicate , return. return bag.cast<t>().where(predicate).tolist(); } // return empty list. return new list<t>(); }
of course need handle adding items cache ensure different types put corresponding bags.
Comments
Post a Comment