mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
ReentrantCache修改get逻辑key锁改为全局锁,保证安全。(issue#4022@Github)
This commit is contained in:
@@ -22,7 +22,7 @@
|
||||
* 【extra 】 修复`QLExpressEngine`allowClassSet无效问题(issue#3994@Github)
|
||||
* 【core 】 修复`StrBuilder`insert插入计算错误问题(issue#ICTSRZ@Gitee)
|
||||
* 【cron 】 修复`CronPatternUtil.nextDateAfter`计算下一个匹配表达式的日期时,计算错误问题(issue#4006@Github)
|
||||
* 【cache 】 `ReentrantCache`增加`setUseKeyLock`让用户选择是否使用键锁来增强性能,或者保证安全。(issue#4022@Github)
|
||||
* 【cache 】 `ReentrantCache`修改get逻辑key锁改为全局锁,保证安全。(issue#4022@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.39(2025-06-20)
|
||||
|
||||
@@ -25,20 +25,6 @@ public abstract class ReentrantCache<K, V> extends AbstractCache<K, V> {
|
||||
// TODO 最优的解决方案是使用Guava的ConcurrentLinkedHashMap,此处使用简化的互斥锁
|
||||
protected final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
private boolean useKeyLock = true;
|
||||
|
||||
/**
|
||||
* 设置是否使用key锁,默认为true
|
||||
*
|
||||
* @param useKeyLock 是否使用key锁
|
||||
* @return this
|
||||
* @since 5.8.40
|
||||
*/
|
||||
public ReentrantCache<K, V> setUseKeyLock(boolean useKeyLock) {
|
||||
this.useKeyLock = useKeyLock;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(K key, V object, long timeout) {
|
||||
lock.lock();
|
||||
@@ -61,11 +47,6 @@ public abstract class ReentrantCache<K, V> extends AbstractCache<K, V> {
|
||||
|
||||
@Override
|
||||
public V get(final K key, final boolean isUpdateLastAccess, final long timeout, final Func0<V> valueFactory) {
|
||||
if(useKeyLock){
|
||||
// 用户如果允许,则使用key锁,可以减少valueFactory对象创建造成的
|
||||
return super.get(key, isUpdateLastAccess, timeout, valueFactory);
|
||||
}
|
||||
|
||||
V v = get(key, isUpdateLastAccess);
|
||||
|
||||
// 对象不存在,则加锁创建
|
||||
@@ -80,7 +61,7 @@ public abstract class ReentrantCache<K, V> extends AbstractCache<K, V> {
|
||||
if (null == co) {
|
||||
// supplier的创建是一个耗时过程,此处创建与全局锁无关,而与key锁相关,这样就保证每个key只创建一个value,且互斥
|
||||
v = valueFactory.callWithRuntimeException();
|
||||
put(key, v, timeout);
|
||||
putWithoutLock(key, v, timeout);
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
|
||||
Reference in New Issue
Block a user