Skip to content

Commit ab291d7

Browse files
committed
Optimistic lock approach to ensure sync. over group values on clear
1 parent b15bc13 commit ab291d7

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

src/main/java/org/mybatis/caches/memcached/MemcachedClientWrapper.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class MemcachedClientWrapper {
4747

4848
/**
4949
* Used to represent an object retrieved from Memcached along with its CAS information
50-
*
50+
*
5151
* @author Weisz, Gustavo E.
5252
*/
5353
private class ObjectWithCas {
@@ -95,7 +95,7 @@ public MemcachedClientWrapper() {
9595

9696
/**
9797
* Converts the MyBatis object key in the proper string representation.
98-
*
98+
*
9999
* @param key
100100
* the MyBatis object key.
101101
* @return the proper string representation.
@@ -195,7 +195,7 @@ private Object retrieve(final String keyString) {
195195

196196
/**
197197
* Retrieves an object along with its cas using the given key
198-
*
198+
*
199199
* @param keyString
200200
* @return
201201
* @throws Exception
@@ -292,9 +292,9 @@ private void storeInMemcached(String keyString, Object value) {
292292

293293
/**
294294
* Tries to update an object value in memcached considering the cas validation
295-
*
295+
*
296296
* Returns true if the object passed the cas validation and was modified.
297-
*
297+
*
298298
* @param keyString
299299
* @param value
300300
* @return
@@ -319,9 +319,9 @@ private boolean storeInMemcached(String keyString, ObjectWithCas value) {
319319

320320
/**
321321
* Tries to store an object identified by a key in Memcached.
322-
*
322+
*
323323
* Will fail if the object already exists.
324-
*
324+
*
325325
* @param keyString
326326
* @param value
327327
* @return
@@ -370,31 +370,40 @@ public Object removeObject(Object key) {
370370
public void removeGroup(String id) {
371371
String groupKey = toKeyString(id);
372372

373-
ObjectWithCas group = getGroup(groupKey);
374-
Set<String> groupValues;
373+
// remove namespace key into memcached
374+
// Optimistic lock approach...
375+
boolean jobDone = false;
376+
377+
while (!jobDone) {
378+
ObjectWithCas group = getGroup(groupKey);
379+
Set<String> groupValues;
380+
381+
if (group == null || group.getObject() == null) {
382+
if (LOG.isDebugEnabled()) {
383+
LOG.debug("No need to flush cached entries for group '" + id + "' because is empty");
384+
}
385+
return;
386+
}
375387

376-
if (group == null || group.getObject() == null) {
377388
if (LOG.isDebugEnabled()) {
378-
LOG.debug("No need to flush cached entries for group '" + id + "' because is empty");
389+
LOG.debug("Flushing keys: " + group);
379390
}
380-
return;
381-
}
382391

383-
if (LOG.isDebugEnabled()) {
384-
LOG.debug("Flushing keys: " + group);
385-
}
392+
groupValues = (Set<String>) group.getObject();
386393

387-
groupValues = (Set<String>) group.getObject();
394+
for (String key : groupValues) {
395+
client.delete(key);
396+
}
388397

389-
for (String key : groupValues) {
390-
client.delete(key);
391-
}
398+
if (LOG.isDebugEnabled()) {
399+
LOG.debug("Flushing group: " + groupKey);
400+
}
392401

393-
if (LOG.isDebugEnabled()) {
394-
LOG.debug("Flushing group: " + groupKey);
395-
}
402+
groupValues = (Set<String>) group.getObject();
403+
groupValues.clear();
396404

397-
client.delete(groupKey);
405+
jobDone = storeInMemcached(groupKey, group);
406+
}
398407
}
399408

400409
@Override

0 commit comments

Comments
 (0)