mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复AbstractCache
putWithoutLock方法可能导致的外部资源泄露问题(pr#3958@Github)
This commit is contained in:
@@ -88,12 +88,10 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
|
||||
final MutableObj<K> mKey = MutableObj.of(key);
|
||||
|
||||
// issue#3618 对于替换的键值对,不做满队列检查和清除
|
||||
if (cacheMap.containsKey(mKey)) {
|
||||
CacheObj<K, V> oldObj = cacheMap.get(mKey);
|
||||
if (oldObj != null) {
|
||||
onRemove(oldObj.key, oldObj.obj);
|
||||
cacheMap.remove(mKey);
|
||||
}
|
||||
final CacheObj<K, V> oldObj = cacheMap.get(mKey);
|
||||
if (null != oldObj) {
|
||||
onRemove(oldObj.key, oldObj.obj);
|
||||
// 存在相同key,覆盖之
|
||||
cacheMap.put(mKey, co);
|
||||
} else {
|
||||
if (isFull()) {
|
||||
|
@@ -4,13 +4,13 @@ import cn.hutool.cache.impl.TimedCache;
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 缓存测试用例
|
||||
*
|
||||
@@ -155,19 +155,15 @@ public class CacheTest {
|
||||
*/
|
||||
@Test
|
||||
public void reentrantCache_clear_Method_Test() {
|
||||
Cache<String, String> lruCache = CacheUtil.newLRUCache(4);
|
||||
lruCache.setListener(new CacheListener<String, String>() {
|
||||
@Override
|
||||
public void onRemove(String key, String cachedObject) {
|
||||
System.out.println(" listener key= "+ key+" value = "+cachedObject.toString()+" cachedObject资源释放操作...");
|
||||
}
|
||||
} );
|
||||
final AtomicInteger removeCount = new AtomicInteger();
|
||||
final Cache<String, String> lruCache = CacheUtil.newLRUCache(4);
|
||||
lruCache.setListener((key, cachedObject) -> removeCount.getAndIncrement());
|
||||
lruCache.put("key1","String1");
|
||||
lruCache.put("key2","String2");
|
||||
lruCache.put("key3","String3");
|
||||
lruCache.put("key1","String4");//key已经存在,原始putWithoutLock方法存在资源泄露
|
||||
lruCache.put("key4","String5");
|
||||
lruCache.clear();//ReentrantCache类clear()方法存在资源泄露
|
||||
|
||||
Assertions.assertEquals(5, removeCount.get());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user