fix concurrent modification exceptions in local cache

This commit is contained in:
fjy 2014-04-08 16:23:24 -07:00
parent 7c90a0fb96
commit dd8e9e2930
1 changed files with 11 additions and 5 deletions

View File

@ -77,7 +77,10 @@ public class MapCache implements Cache
@Override @Override
public byte[] get(NamedKey key) public byte[] get(NamedKey key)
{ {
final byte[] retVal = baseMap.get(computeKey(getNamespaceId(key.namespace), key.key)); final byte[] retVal;
synchronized (clearLock) {
retVal = baseMap.get(computeKey(getNamespaceId(key.namespace), key.key));
}
if (retVal == null) { if (retVal == null) {
missCount.incrementAndGet(); missCount.incrementAndGet();
} else { } else {
@ -90,7 +93,7 @@ public class MapCache implements Cache
public void put(NamedKey key, byte[] value) public void put(NamedKey key, byte[] value)
{ {
synchronized (clearLock) { synchronized (clearLock) {
baseMap.put(computeKey(getNamespaceId(key.namespace), key.key), value); baseMap.put(computeKey(getNamespaceId(key.namespace), key.key), value);
} }
} }
@ -98,7 +101,7 @@ public class MapCache implements Cache
public Map<NamedKey, byte[]> getBulk(Iterable<NamedKey> keys) public Map<NamedKey, byte[]> getBulk(Iterable<NamedKey> keys)
{ {
Map<NamedKey, byte[]> retVal = Maps.newHashMap(); Map<NamedKey, byte[]> retVal = Maps.newHashMap();
for(NamedKey key : keys) { for (NamedKey key : keys) {
retVal.put(key, get(key)); retVal.put(key, get(key));
} }
return retVal; return retVal;
@ -110,7 +113,9 @@ public class MapCache implements Cache
byte[] idBytes; byte[] idBytes;
synchronized (namespaceId) { synchronized (namespaceId) {
idBytes = getNamespaceId(namespace); idBytes = getNamespaceId(namespace);
if(idBytes == null) return; if (idBytes == null) {
return;
}
namespaceId.remove(namespace); namespaceId.remove(namespace);
} }
@ -150,7 +155,8 @@ public class MapCache implements Cache
return retVal; return retVal;
} }
public boolean isLocal() { public boolean isLocal()
{
return true; return true;
} }
} }