修复LRUCache移除事件监听失效

This commit is contained in:
Looly
2022-10-12 00:11:32 +08:00
parent 09e8d7c6d1
commit c12102e89b
4 changed files with 64 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
package cn.hutool.cache.impl;
import cn.hutool.core.lang.mutable.Mutable;
import cn.hutool.core.map.FixedLinkedHashMap;
import java.util.Iterator;
@@ -42,7 +43,13 @@ public class LRUCache<K, V> extends ReentrantCache<K, V> {
this.timeout = timeout;
//链表key按照访问顺序排序调用get方法后会将这次访问的元素移至头部
cacheMap = new FixedLinkedHashMap<>(capacity);
final FixedLinkedHashMap<Mutable<K>, CacheObj<K, V>> fixedLinkedHashMap = new FixedLinkedHashMap<>(capacity);
fixedLinkedHashMap.setRemoveListener(entry -> {
if(null != listener){
listener.onRemove(entry.getKey().get(), entry.getValue().getValue());
}
});
cacheMap = fixedLinkedHashMap;
}
// ---------------------------------------------------------------- prune