提交 e71508b9 authored 作者: Thomas Mueller's avatar Thomas Mueller

Support notification on entry removal

上级 cf0726c6
...@@ -106,7 +106,7 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> { ...@@ -106,7 +106,7 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> {
long max = Math.max(1, maxMemory / segmentCount); long max = Math.max(1, maxMemory / segmentCount);
for (int i = 0; i < segmentCount; i++) { for (int i = 0; i < segmentCount; i++) {
segments[i] = new Segment<K, V>( segments[i] = new Segment<K, V>(
max, averageMemory, stackMoveDistance); this, max, averageMemory, stackMoveDistance);
} }
} }
...@@ -179,6 +179,16 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> { ...@@ -179,6 +179,16 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> {
return averageMemory; return averageMemory;
} }
/**
* This method is called after the value for the given key was removed.
* It is not called on clear or put when replacing a value.
*
* @param key the key
*/
protected void onRemove(K key) {
// do nothing
}
/** /**
* Remove an entry. Both resident and non-resident entries can be * Remove an entry. Both resident and non-resident entries can be
* removed. * removed.
...@@ -413,6 +423,8 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> { ...@@ -413,6 +423,8 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> {
*/ */
static class Segment<K, V> { static class Segment<K, V> {
final CacheLIRS<K, V> cache;
/** /**
* The number of (hot, cold, and non-resident) entries in the map. * The number of (hot, cold, and non-resident) entries in the map.
*/ */
...@@ -500,7 +512,8 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> { ...@@ -500,7 +512,8 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> {
* @param stackMoveDistance the number of other entries to be moved to * @param stackMoveDistance the number of other entries to be moved to
* the top of the stack before moving an entry to the top * the top of the stack before moving an entry to the top
*/ */
Segment(long maxMemory, int averageMemory, int stackMoveDistance) { Segment(CacheLIRS<K, V> cache, long maxMemory, int averageMemory, int stackMoveDistance) {
this.cache = cache;
setMaxMemory(maxMemory); setMaxMemory(maxMemory);
setAverageMemory(averageMemory); setAverageMemory(averageMemory);
this.stackMoveDistance = stackMoveDistance; this.stackMoveDistance = stackMoveDistance;
...@@ -748,6 +761,7 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> { ...@@ -748,6 +761,7 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> {
Entry<K, V> e = queue.queuePrev; Entry<K, V> e = queue.queuePrev;
usedMemory -= e.memory; usedMemory -= e.memory;
removeFromQueue(e); removeFromQueue(e);
cache.onRemove(e.key);
e.value = null; e.value = null;
e.memory = 0; e.memory = 0;
addToQueue(queue2, e); addToQueue(queue2, e);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论