memcached - Compare and Swap (CAS) implementation in EhCache -
i trying find equivalent of memcache's casmutator.cas in ehcache. essentially, swapping out ehcache memcache , need implement interface calls setting value via cas. have insight this? also, given i'm not purporting expert in of this, if has high level overview of how cas works / doing, appreciated well.
the compare , swap method equivalent in ehcache replace(element old, element element) method found in net.sf.ehcache.cache. method compares "old" element against element in cache and, if matches, replaces element in cache "element." following method provides simple usage example assumes "acache" cache object method has access , "acache" used cache objects of type long.
// replace cached value associated key newvalue , // return original value public long replace(string key, long newvalue, long maxtries) boolean success = false; long originalvalue; element originalelement; element newelement = new element(key, newvalue); (int ii = 0; !success && ii < maxtries; ++ii) { // copy of original list originalvalue = (long) acache.get(key).getvalue(); // make duplicate of element exists "key" originalelement = new element(key, originalvalue); // if value inkey has not changed since setting originalvalue, // replace value "key" "newvalue" if (acache.replace(originalelement, newelement)) { success = true; } } if (!success) { originalvalue = null; } return originalvalue; }
note works if key exists in cache. if doesn't, call acache.replace returns false , not put newelement cache. if dig far enough internals of ehcache (the replace method of segment class in net.sf.ehcache.store.compound package), you'll find replace implemented aquiring write lock. said, presumed aquiring write lock no different using replace method. such, theoretically replace whole function calling acache.aquirewritelockonkey, performing needed actions, , releasing write lock.
an overview of compare-and-swap can found on wikipedia at: http://en.wikipedia.org/wiki/compare-and-swap.
Comments
Post a Comment