Fixed race condition during cache entry removing. #592

pull/605/head
Nikita 9 years ago
parent 60b1c8fa15
commit ebec4744c0

@ -102,9 +102,9 @@ public class LFUCacheMap<K, V> extends AbstractCacheMap<K, V> {
@Override
protected void onValueRemove(CachedValue value) {
MapKey key = toKey((LFUCachedValue)value);
if (accessMap.remove(key) == null) {
throw new IllegalStateException();
synchronized (value) {
MapKey key = toKey((LFUCachedValue)value);
accessMap.remove(key);
}
}
@ -116,7 +116,7 @@ public class LFUCacheMap<K, V> extends AbstractCacheMap<K, V> {
MapKey key = toKey(value);
if (accessMap.remove(key) == null) {
throw new IllegalStateException();
return;
}
if (count < 0) {
@ -132,6 +132,9 @@ public class LFUCacheMap<K, V> extends AbstractCacheMap<K, V> {
@Override
protected void onMapFull() {
Map.Entry<MapKey, LFUCachedValue> entry = accessMap.pollFirstEntry();
if (entry == null) {
return;
}
map.remove(entry.getValue().getKey(), entry.getValue());
if (entry.getValue().accessCount == 0) {

@ -41,22 +41,27 @@ public class LRUCacheMap<K, V> extends AbstractCacheMap<K, V> {
@Override
protected void onValueRemove(CachedValue value) {
queue.remove(value);
synchronized (value) {
queue.remove(value);
}
}
@Override
protected void onValueRead(CachedValue value) {
// move value to tail of queue
synchronized (value) {
queue.remove(value);
queue.add(value);
if (queue.remove(value)) {
queue.add(value);
}
}
}
@Override
protected void onMapFull() {
CachedValue value = queue.poll();
map.remove(value.getKey(), value);
if (value != null) {
map.remove(value.getKey(), value);
}
}
@Override

Loading…
Cancel
Save