提交 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> {
long max = Math.max(1, maxMemory / segmentCount);
for (int i = 0; i < segmentCount; i++) {
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> {
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
* removed.
......@@ -413,6 +423,8 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> {
*/
static class Segment<K, V> {
final CacheLIRS<K, V> cache;
/**
* 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> {
* @param stackMoveDistance the number of other entries to be moved to
* 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);
setAverageMemory(averageMemory);
this.stackMoveDistance = stackMoveDistance;
......@@ -748,6 +761,7 @@ public class CacheLIRS<K, V> extends AbstractMap<K, V> {
Entry<K, V> e = queue.queuePrev;
usedMemory -= e.memory;
removeFromQueue(e);
cache.onRemove(e.key);
e.value = null;
e.memory = 0;
addToQueue(queue2, e);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论