diff --git a/src/main/java/org/mybatis/caches/memcached/MemcachedClientWrapper.java b/src/main/java/org/mybatis/caches/memcached/MemcachedClientWrapper.java index 7d97210..94494cd 100644 --- a/src/main/java/org/mybatis/caches/memcached/MemcachedClientWrapper.java +++ b/src/main/java/org/mybatis/caches/memcached/MemcachedClientWrapper.java @@ -47,7 +47,7 @@ final class MemcachedClientWrapper { /** * Used to represent an object retrieved from Memcached along with its CAS information - * + * * @author Weisz, Gustavo E. */ private class ObjectWithCas { @@ -95,7 +95,7 @@ public MemcachedClientWrapper() { /** * Converts the MyBatis object key in the proper string representation. - * + * * @param key * the MyBatis object key. * @return the proper string representation. @@ -195,7 +195,7 @@ private Object retrieve(final String keyString) { /** * Retrieves an object along with its cas using the given key - * + * * @param keyString * @return * @throws Exception @@ -292,9 +292,9 @@ private void storeInMemcached(String keyString, Object value) { /** * Tries to update an object value in memcached considering the cas validation - * + * * Returns true if the object passed the cas validation and was modified. - * + * * @param keyString * @param value * @return @@ -319,9 +319,9 @@ private boolean storeInMemcached(String keyString, ObjectWithCas value) { /** * Tries to store an object identified by a key in Memcached. - * + * * Will fail if the object already exists. - * + * * @param keyString * @param value * @return @@ -370,31 +370,40 @@ public Object removeObject(Object key) { public void removeGroup(String id) { String groupKey = toKeyString(id); - ObjectWithCas group = getGroup(groupKey); - Set groupValues; + // remove namespace key into memcached + // Optimistic lock approach... + boolean jobDone = false; + + while (!jobDone) { + ObjectWithCas group = getGroup(groupKey); + Set groupValues; + + if (group == null || group.getObject() == null) { + if (LOG.isDebugEnabled()) { + LOG.debug("No need to flush cached entries for group '" + id + "' because is empty"); + } + return; + } - if (group == null || group.getObject() == null) { if (LOG.isDebugEnabled()) { - LOG.debug("No need to flush cached entries for group '" + id + "' because is empty"); + LOG.debug("Flushing keys: " + group); } - return; - } - if (LOG.isDebugEnabled()) { - LOG.debug("Flushing keys: " + group); - } + groupValues = (Set) group.getObject(); - groupValues = (Set) group.getObject(); + for (String key : groupValues) { + client.delete(key); + } - for (String key : groupValues) { - client.delete(key); - } + if (LOG.isDebugEnabled()) { + LOG.debug("Flushing group: " + groupKey); + } - if (LOG.isDebugEnabled()) { - LOG.debug("Flushing group: " + groupKey); - } + groupValues = (Set) group.getObject(); + groupValues.clear(); - client.delete(groupKey); + jobDone = storeInMemcached(groupKey, group); + } } @Override