asp.net - List<T> on Cache (Security Operations) -
i'm developing web app using asp.net mvc.
i have list in cache (list of product class of model). so, crud operations on cached list. know httpcontext.current.cache typesafe list no. i'd know, best way operations thread safe avoid conflicts ?
my code this:
public static class projectcached { const string key = "productlist"; // cached list public static ilist<product> products { { if (httpcontext.current.cache[key] == null) { httpcontext.current.cache.insert(key, /* data database */, ...); } return (list<product>)httpcontext.current.cache[key]; } } // methods (operations on cached list) public static void increase(long id) { var item = products.firstordefault(x => x.id == id); if (item != null) { item.prince += 1; item.lastupdate = datetime.now; } } public static void remove(long id) { var item = products.firstordefault(x => x.id == id); if (item != null) { products.remove(item); } } // others methods
}
i have lot of users writing on cached list @ same time...
i know can use "lock" keyword need put ? on object finded? on list?
thanks
consider using idictionary instead of list, example sortedlist<tkey, tvalue>
safe-threading: can using
lock
keywordreaderwriterlockslim - lock when needed
concurrentdictionary<tkey, tvalue> - if thread-safe implemented in enough you.
upd: readerwriterlockslim
more advanced simple lock
keyword. using readerwriterlockslim
allows many readers data simultaneously without locks. when thread tries write data, other threads locked , reading , writing.
Comments
Post a Comment