c# - what's the correct way to use locking in AppFabric -
i've got service updates appfabric cache 8000 objects every 4 hours. read table, creates object every record , stores in cache.
while cache being updated 1 object @ time consumers can still request objects cache. there's not information available this, want know if following code correct way lock , store cache objects in appfabric while other threads can query cache:
// caching part private static void cachedealers(dictionary<key<string>, dealer> dealers) { datacache cache = cache.instance.dealercache; foreach (key<string> key in dealers.keys) { cacheobject(cache, key.tostring(), dealers[key], mdealerregionname); } } private static void cacheobject(datacache cache, string key, object obj, string region) { datacachelockhandle lockhandle; if (cache.getandlock(key, timespan.fromseconds(10), out lockhandle) == null) { cache.put(key, obj, timespan.maxvalue, region); } else { cache.putandunlock(key, obj, lockhandle, timespan.maxvalue, region); } } // retrieve part public dealer getdealer(string lab, string number) { dealer dealer = (dealer)cache.instance.dealercache.get(new key<string>(lab, number).tostring(), mdealerregionname); return dealer; }
a locked item can still read cache (though while it's locked return original object, not updated one), getdealer
method should continue work while cache being updated. msdn:
regular method calls not blocked , access latest version of cached object
Comments
Post a Comment