This commit is contained in:
Looly 2024-03-18 20:27:14 +08:00
parent 86442f35c8
commit 017ec30fe6
49 changed files with 116 additions and 94 deletions

View File

@ -15,7 +15,7 @@ package org.dromara.hutool.core.annotation;
import org.dromara.hutool.core.annotation.elements.HierarchicalAnnotatedElements;
import org.dromara.hutool.core.annotation.elements.MetaAnnotatedElement;
import org.dromara.hutool.core.annotation.elements.RepeatableMetaAnnotatedElement;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.util.ObjUtil;
@ -133,7 +133,7 @@ import java.util.stream.Stream;
* <p><strong>缓存</strong>
* <p>为了避免注解以及{@link AnnotatedElement}层级结构解析过程中的大量反射调用
* 工具类为{@link AnnotatedElement}及其元注解信息进行了缓存<br>
* 缓存功能默认基于{@link WeakConcurrentMap}实现会在gc时自动回收部分缓存数据
* 缓存功能默认基于{@link WeakKeyConcurrentMap}实现会在gc时自动回收部分缓存数据
* 但是若有必要也可以调用{@link #clearCaches()}方法主动清空缓存
*
* @author huangchengxing
@ -150,22 +150,22 @@ public class AnnotatedElementUtil {
/**
* 支持属性解析的{@link MetaAnnotatedElement}缓存
*/
private static final Map<AnnotatedElement, MetaAnnotatedElement<ResolvedAnnotationMapping>> RESOLVED_ELEMENT_CACHE = new WeakConcurrentMap<>();
private static final Map<AnnotatedElement, MetaAnnotatedElement<ResolvedAnnotationMapping>> RESOLVED_ELEMENT_CACHE = new WeakKeyConcurrentMap<>();
/**
* 不支持属性解析的{@link MetaAnnotatedElement}缓存
*/
private static final Map<AnnotatedElement, MetaAnnotatedElement<GenericAnnotationMapping>> ELEMENT_CACHE = new WeakConcurrentMap<>();
private static final Map<AnnotatedElement, MetaAnnotatedElement<GenericAnnotationMapping>> ELEMENT_CACHE = new WeakKeyConcurrentMap<>();
/**
* 不支持属性解析的{@link RepeatableMetaAnnotatedElement}缓存
*/
private static final Map<AnnotatedElement, RepeatableMetaAnnotatedElement<ResolvedAnnotationMapping>> RESOLVED_REPEATABLE_ELEMENT_CACHE = new WeakConcurrentMap<>();
private static final Map<AnnotatedElement, RepeatableMetaAnnotatedElement<ResolvedAnnotationMapping>> RESOLVED_REPEATABLE_ELEMENT_CACHE = new WeakKeyConcurrentMap<>();
/**
* 不支持属性解析的{@link RepeatableMetaAnnotatedElement}缓存
*/
private static final Map<AnnotatedElement, RepeatableMetaAnnotatedElement<GenericAnnotationMapping>> REPEATABLE_ELEMENT_CACHE = new WeakConcurrentMap<>();
private static final Map<AnnotatedElement, RepeatableMetaAnnotatedElement<GenericAnnotationMapping>> REPEATABLE_ELEMENT_CACHE = new WeakKeyConcurrentMap<>();
// region ========== find ==========

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.core.annotation;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.reflect.method.MethodUtil;
import org.dromara.hutool.core.text.CharSequenceUtil;

View File

@ -19,7 +19,7 @@ import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.func.LambdaInfo;
import org.dromara.hutool.core.func.LambdaUtil;
import org.dromara.hutool.core.func.SerFunction;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.reflect.FieldUtil;
import org.dromara.hutool.core.reflect.method.MethodUtil;
import org.dromara.hutool.core.text.CharSequenceUtil;
@ -50,7 +50,7 @@ public class AnnotationUtil {
/**
* 直接声明的注解缓存
*/
private static final Map<AnnotatedElement, Annotation[]> DECLARED_ANNOTATIONS_CACHE = new WeakConcurrentMap<>();
private static final Map<AnnotatedElement, Annotation[]> DECLARED_ANNOTATIONS_CACHE = new WeakKeyConcurrentMap<>();
/**
* 获取直接声明的注解若已有缓存则从缓存中获取

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.core.annotation;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.reflect.method.MethodUtil;
import org.dromara.hutool.core.text.CharSequenceUtil;
@ -337,7 +337,7 @@ public interface RepeatableAnnotationCollector {
/**
* 可重复注解对应的方法缓存
*/
private final Map<Class<? extends Annotation>, Object> repeatableMethodCache = new WeakConcurrentMap<>();
private final Map<Class<? extends Annotation>, Object> repeatableMethodCache = new WeakKeyConcurrentMap<>();
/**
* 构造
@ -447,7 +447,7 @@ public interface RepeatableAnnotationCollector {
/**
* 可重复注解对应的方法缓存
*/
private final Map<Class<? extends Annotation>, Object> repeatableMethodCache = new WeakConcurrentMap<>();
private final Map<Class<? extends Annotation>, Object> repeatableMethodCache = new WeakKeyConcurrentMap<>();
/**
* 构造

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.core.bean;
import org.dromara.hutool.core.func.SerSupplier;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
/**
* Bean属性缓存<br>
@ -27,7 +27,7 @@ public enum BeanDescCache {
*/
INSTANCE;
private final WeakConcurrentMap<Class<?>, BeanDesc> bdCache = new WeakConcurrentMap<>();
private final WeakKeyConcurrentMap<Class<?>, BeanDesc> bdCache = new WeakKeyConcurrentMap<>();
/**
* 获得属性名和{@link BeanDesc}Map映射

View File

@ -13,8 +13,8 @@
package org.dromara.hutool.core.bean;
import org.dromara.hutool.core.func.SerSupplier;
import org.dromara.hutool.core.map.ReferenceConcurrentMap;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.ReferenceKeyConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import java.beans.PropertyDescriptor;
import java.util.Map;
@ -31,8 +31,8 @@ public enum BeanInfoCache {
*/
INSTANCE;
private final WeakConcurrentMap<Class<?>, Map<String, PropertyDescriptor>> pdCache = new WeakConcurrentMap<>();
private final WeakConcurrentMap<Class<?>, Map<String, PropertyDescriptor>> ignoreCasePdCache = new WeakConcurrentMap<>();
private final WeakKeyConcurrentMap<Class<?>, Map<String, PropertyDescriptor>> pdCache = new WeakKeyConcurrentMap<>();
private final WeakKeyConcurrentMap<Class<?>, Map<String, PropertyDescriptor>> ignoreCasePdCache = new WeakKeyConcurrentMap<>();
/**
* 获得属性名和{@link PropertyDescriptor}Map映射
@ -86,10 +86,10 @@ public enum BeanInfoCache {
* 根据是否忽略字段名的大小写返回不用Cache对象
*
* @param ignoreCase 是否忽略大小写
* @return {@link ReferenceConcurrentMap}
* @return {@link ReferenceKeyConcurrentMap}
* @since 5.4.1
*/
private ReferenceConcurrentMap<Class<?>, Map<String, PropertyDescriptor>> getCache(final boolean ignoreCase) {
private ReferenceKeyConcurrentMap<Class<?>, Map<String, PropertyDescriptor>> getCache(final boolean ignoreCase) {
return ignoreCase ? ignoreCasePdCache : pdCache;
}
}

View File

@ -17,8 +17,8 @@ import org.dromara.hutool.core.func.SerSupplier;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.mutable.Mutable;
import org.dromara.hutool.core.lang.mutable.MutableObj;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import java.io.Serializable;
import java.util.Iterator;
@ -33,7 +33,7 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* 简单缓存无超时实现默认使用{@link WeakConcurrentMap}实现缓存自动清理
* 简单缓存无超时实现默认使用{@link WeakKeyConcurrentMap}实现缓存自动清理
*
* @param <K> 键类型
* @param <V> 值类型
@ -57,7 +57,7 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
* 构造默认使用{@link WeakHashMap}实现缓存自动清理
*/
public SimpleCache() {
this(new WeakConcurrentMap<>());
this(new WeakKeyConcurrentMap<>());
}
/**

View File

@ -17,7 +17,7 @@ import org.dromara.hutool.core.cache.CacheListener;
import org.dromara.hutool.core.func.SerSupplier;
import org.dromara.hutool.core.lang.mutable.Mutable;
import org.dromara.hutool.core.lang.mutable.MutableObj;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import java.util.Iterator;
import java.util.Map;

View File

@ -15,7 +15,7 @@ package org.dromara.hutool.core.cache.impl;
import org.dromara.hutool.core.cache.CacheListener;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.lang.mutable.Mutable;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import java.lang.ref.Reference;
@ -39,14 +39,14 @@ public class WeakCache<K, V> extends TimedCache<K, V>{
* @param timeout 超时时常单位毫秒-1或0表示无限制
*/
public WeakCache(final long timeout) {
super(timeout, new WeakConcurrentMap<>());
super(timeout, new WeakKeyConcurrentMap<>());
}
@Override
public WeakCache<K, V> setListener(final CacheListener<K, V> listener) {
super.setListener(listener);
final WeakConcurrentMap<Mutable<K>, CacheObj<K, V>> map = (WeakConcurrentMap<Mutable<K>, CacheObj<K, V>>) this.cacheMap;
final WeakKeyConcurrentMap<Mutable<K>, CacheObj<K, V>> map = (WeakKeyConcurrentMap<Mutable<K>, CacheObj<K, V>>) this.cacheMap;
// WeakKey回收之后key对应的值已经是null了因此此处的key也为null
map.setPurgeListener((key, value)-> listener.onRemove(Opt.ofNullable(key).map(Reference::get).map(Mutable::get).get(), value.getValue()));

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.core.collection;
import org.dromara.hutool.core.collection.set.SetFromMap;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import java.util.Collection;

View File

@ -17,7 +17,7 @@ import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.lang.tuple.Pair;
import org.dromara.hutool.core.lang.tuple.Triple;
import org.dromara.hutool.core.lang.tuple.Tuple;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.reflect.TypeUtil;
import javax.xml.datatype.XMLGregorianCalendar;

View File

@ -16,7 +16,7 @@ import org.dromara.hutool.core.convert.AbstractConverter;
import org.dromara.hutool.core.convert.ConvertException;
import org.dromara.hutool.core.lang.EnumItem;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.reflect.ClassUtil;
import org.dromara.hutool.core.reflect.method.MethodUtil;
import org.dromara.hutool.core.reflect.ModifierUtil;
@ -42,7 +42,7 @@ public class EnumConverter extends AbstractConverter {
*/
public static final EnumConverter INSTANCE = new EnumConverter();
private static final WeakConcurrentMap<Class<?>, Map<Class<?>, Method>> VALUE_OF_METHOD_CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Class<?>, Map<Class<?>, Method>> VALUE_OF_METHOD_CACHE = new WeakKeyConcurrentMap<>();
@Override
protected Object convertInternal(final Class<?> targetClass, final Object value) {

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.core.data;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.regex.PatternPool;
import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.core.text.StrUtil;

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.core.date.format;
import org.dromara.hutool.core.date.DateException;
import org.dromara.hutool.core.date.format.parser.FastDateParser;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import java.io.IOException;
import java.io.ObjectInputStream;

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.core.date.format;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.tuple.Tuple;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import java.text.DateFormat;
import java.text.Format;

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.core.date.format;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

View File

@ -16,7 +16,7 @@ import org.dromara.hutool.core.date.DateException;
import org.dromara.hutool.core.date.format.FastDateFormat;
import org.dromara.hutool.core.date.format.FastDatePrinter;
import org.dromara.hutool.core.date.format.SimpleDateBasic;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import java.io.IOException;
import java.io.ObjectInputStream;

View File

@ -15,7 +15,7 @@ package org.dromara.hutool.core.func;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.mutable.MutableEntry;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.reflect.*;
import org.dromara.hutool.core.reflect.lookup.LookupUtil;
import org.dromara.hutool.core.reflect.method.MethodTypeUtil;
@ -37,7 +37,7 @@ public class LambdaFactory {
throw new IllegalAccessException();
}
private static final Map<MutableEntry<Class<?>, Executable>, Object> CACHE = new WeakConcurrentMap<>();
private static final Map<MutableEntry<Class<?>, Executable>, Object> CACHE = new WeakKeyConcurrentMap<>();
/**
* 构建Lambda

View File

@ -17,7 +17,7 @@ import org.dromara.hutool.core.classloader.ClassLoaderUtil;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.reflect.ClassDescUtil;
import org.dromara.hutool.core.reflect.method.MethodUtil;
import org.dromara.hutool.core.reflect.ModifierUtil;
@ -37,7 +37,7 @@ import java.util.function.*;
*/
public class LambdaUtil {
private static final WeakConcurrentMap<Object, LambdaInfo> CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Object, LambdaInfo> CACHE = new WeakKeyConcurrentMap<>();
/**
* 通过对象的方法或类的静态方法引用获取lambda实现类

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.core.lang;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.func.SerSupplier;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.text.StrUtil;

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.core.lang.intern;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import java.lang.ref.WeakReference;
@ -25,7 +25,7 @@ import java.lang.ref.WeakReference;
*/
public class WeakIntern<T> implements Intern<T> {
private final WeakConcurrentMap<T, WeakReference<T>> cache = new WeakConcurrentMap<>();
private final WeakKeyConcurrentMap<T, WeakReference<T>> cache = new WeakKeyConcurrentMap<>();
@Override
public T intern(final T sample) {

View File

@ -18,6 +18,7 @@ import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.collection.iter.ArrayIter;
import org.dromara.hutool.core.collection.iter.IterUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;

View File

@ -15,7 +15,6 @@ import org.dromara.hutool.core.collection.queue.DiscardingQueue;
import org.dromara.hutool.core.collection.queue.Linked;
import org.dromara.hutool.core.collection.queue.LinkedDeque;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.util.RuntimeUtil;
import java.io.InvalidObjectException;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Copyright (c) 2023-2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
@ -10,8 +10,9 @@
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.map;
package org.dromara.hutool.core.map.concurrent;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.util.JdkUtil;
import java.util.Map;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Copyright (c) 2023-2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.map;
package org.dromara.hutool.core.map.reference;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.util.ObjUtil;
@ -40,9 +40,8 @@ import java.util.stream.Collectors;
* @param <K> 键类型
* @param <V> 值类型
* @author looly
* @since 5.8.0
*/
public class ReferenceConcurrentMap<K, V> implements ConcurrentMap<K, V>, Iterable<Map.Entry<K, V>>, Serializable {
public class ReferenceKeyConcurrentMap<K, V> implements ConcurrentMap<K, V>, Iterable<Map.Entry<K, V>>, Serializable {
private static final long serialVersionUID = 1L;
final ConcurrentMap<Reference<K>, V> raw;
@ -61,7 +60,7 @@ public class ReferenceConcurrentMap<K, V> implements ConcurrentMap<K, V>, Iterab
* @param raw {@link ConcurrentMap}实现
* @param referenceType Reference类型
*/
public ReferenceConcurrentMap(final ConcurrentMap<Reference<K>, V> raw, final ReferenceUtil.ReferenceType referenceType) {
public ReferenceKeyConcurrentMap(final ConcurrentMap<Reference<K>, V> raw, final ReferenceUtil.ReferenceType referenceType) {
this.raw = raw;
this.keyType = referenceType;
lastQueue = new ReferenceQueue<>();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Copyright (c) 2023-2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
@ -10,8 +10,9 @@
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.map;
package org.dromara.hutool.core.map.reference;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.util.ReferenceUtil;
import java.lang.ref.Reference;
@ -25,13 +26,13 @@ import java.util.concurrent.ConcurrentMap;
* @author looly
* @since 6.0.0
*/
public class SoftConcurrentMap<K, V> extends ReferenceConcurrentMap<K, V> {
public class SoftKeyConcurrentMap<K, V> extends ReferenceKeyConcurrentMap<K, V> {
private static final long serialVersionUID = 1L;
/**
* 构造
*/
public SoftConcurrentMap() {
public SoftKeyConcurrentMap() {
this(new SafeConcurrentHashMap<>());
}
@ -40,7 +41,7 @@ public class SoftConcurrentMap<K, V> extends ReferenceConcurrentMap<K, V> {
*
* @param raw {@link ConcurrentMap}实现
*/
public SoftConcurrentMap(final ConcurrentMap<Reference<K>, V> raw) {
public SoftKeyConcurrentMap(final ConcurrentMap<Reference<K>, V> raw) {
super(raw, ReferenceUtil.ReferenceType.SOFT);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Copyright (c) 2023-2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
@ -10,8 +10,9 @@
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.map;
package org.dromara.hutool.core.map.reference;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.util.ReferenceUtil;
import java.lang.ref.Reference;
@ -26,13 +27,13 @@ import java.util.concurrent.ConcurrentMap;
* @author looly
* @since 5.8.0
*/
public class WeakConcurrentMap<K, V> extends ReferenceConcurrentMap<K, V> {
public class WeakKeyConcurrentMap<K, V> extends ReferenceKeyConcurrentMap<K, V> {
private static final long serialVersionUID = 1L;
/**
* 构造
*/
public WeakConcurrentMap() {
public WeakKeyConcurrentMap() {
this(new SafeConcurrentHashMap<>());
}
@ -41,7 +42,7 @@ public class WeakConcurrentMap<K, V> extends ReferenceConcurrentMap<K, V> {
*
* @param raw {@link ConcurrentMap}实现
*/
public WeakConcurrentMap(final ConcurrentMap<Reference<K>, V> raw) {
public WeakKeyConcurrentMap(final ConcurrentMap<Reference<K>, V> raw) {
super(raw, ReferenceUtil.ReferenceType.WEAK);
}
}

View File

@ -0,0 +1,18 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* 弱引用Map实现
*
* @author Looly
*/
package org.dromara.hutool.core.map.reference;

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.core.reflect;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
@ -29,7 +29,7 @@ import java.util.Map;
*/
public class ActualTypeMapperPool {
private static final WeakConcurrentMap<Type, Map<Type, Type>> CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Type, Map<Type, Type>> CACHE = new WeakKeyConcurrentMap<>();
/**
* 获取泛型变量和泛型实际类型的对应关系Map

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.core.reflect;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.reflect.creator.DefaultObjectCreator;
import org.dromara.hutool.core.reflect.creator.PossibleObjectCreator;
@ -29,7 +29,7 @@ public class ConstructorUtil {
/**
* 构造对象缓存
*/
private static final WeakConcurrentMap<Class<?>, Constructor<?>[]> CONSTRUCTORS_CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Class<?>, Constructor<?>[]> CONSTRUCTORS_CACHE = new WeakKeyConcurrentMap<>();
// --------------------------------------------------------------------------------------------------------- Constructor
/**

View File

@ -17,7 +17,7 @@ import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.array.ArrayUtil;
@ -36,7 +36,7 @@ public class FieldUtil {
/**
* 字段缓存
*/
private static final WeakConcurrentMap<Class<?>, Field[]> FIELDS_CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Class<?>, Field[]> FIELDS_CACHE = new WeakKeyConcurrentMap<>();
// --------------------------------------------------------------------------------------------------------- Field

View File

@ -16,7 +16,7 @@ import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.lang.mutable.Mutable;
import org.dromara.hutool.core.lang.mutable.MutableObj;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.reflect.ClassUtil;
import java.lang.reflect.Method;
@ -63,7 +63,7 @@ import java.util.function.Predicate;
* <p><strong>缓存</strong>
* <p>对于{@link #getDeclaredMethods}{@link #getMethods}方法与基于这两个方法实现的
* 所有{@code xxxFromMethods}{@code xxxFromDeclaredMethods}方法
* 都提供了缓存基于{@link WeakConcurrentMap}的缓存支持<br>
* 都提供了缓存基于{@link WeakKeyConcurrentMap}的缓存支持<br>
* {@link #getAllMethods}与所有{@code xxxFromAllMethods}方法都基于{@link #getDeclaredMethods}实现
* 但是每次全量查找都需要重新遍历类层级结构因此会带来一定的额外的性能损耗<br>
* 缓存在GC时会被回收但是也可以通过{@link #clearCaches}手动清除缓存
@ -87,12 +87,12 @@ public class MethodScanner {
/**
* 方法缓存
*/
private static final WeakConcurrentMap<Class<?>, Method[]> METHODS_CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Class<?>, Method[]> METHODS_CACHE = new WeakKeyConcurrentMap<>();
/**
* 直接声明的方法缓存
*/
private static final WeakConcurrentMap<Class<?>, Method[]> DECLARED_METHODS_CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Class<?>, Method[]> DECLARED_METHODS_CACHE = new WeakKeyConcurrentMap<>();
// region ============= basic =============

View File

@ -22,7 +22,7 @@ import org.dromara.hutool.core.exception.ExceptionUtil;
import org.dromara.hutool.core.exception.HutoolException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Singleton;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.reflect.ClassUtil;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.reflect.ModifierUtil;
@ -44,11 +44,11 @@ public class MethodUtil {
/**
* 方法缓存
*/
private static final WeakConcurrentMap<Class<?>, Method[]> METHODS_CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Class<?>, Method[]> METHODS_CACHE = new WeakKeyConcurrentMap<>();
/**
* 直接声明的方法缓存
*/
private static final WeakConcurrentMap<Class<?>, Method[]> DECLARED_METHODS_CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Class<?>, Method[]> DECLARED_METHODS_CACHE = new WeakKeyConcurrentMap<>();
// --------------------------------------------------------------------------------------------------------- method

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.core.regex;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import java.util.regex.Pattern;
@ -209,7 +209,7 @@ public class PatternPool {
/**
* Pattern池
*/
private static final WeakConcurrentMap<RegexWithFlag, Pattern> POOL = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<RegexWithFlag, Pattern> POOL = new WeakKeyConcurrentMap<>();
/**
* 先从Pattern池中查找正则对应的{@link Pattern}找不到则编译正则表达式并入池

View File

@ -20,7 +20,7 @@ import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.lang.mutable.MutableInt;
import org.dromara.hutool.core.lang.mutable.MutableObj;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.core.text;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.text.split.SplitUtil;
import java.util.ArrayList;

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.core.text.placeholder;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.lang.mutable.MutableEntry;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.text.placeholder.template.NamedPlaceholderStrTemplate;
import org.dromara.hutool.core.text.placeholder.template.SinglePlaceholderStrTemplate;
@ -27,7 +27,7 @@ import java.util.Map;
* @author Looly
*/
public class StrFormatter {
private static final WeakConcurrentMap<Map.Entry<CharSequence, Object>, StrTemplate> CACHE = new WeakConcurrentMap<>();
private static final WeakKeyConcurrentMap<Map.Entry<CharSequence, Object>, StrTemplate> CACHE = new WeakKeyConcurrentMap<>();
/**
* 格式化字符串<br>

View File

@ -12,6 +12,7 @@
package org.dromara.hutool.core.map;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;

View File

@ -12,6 +12,7 @@
package org.dromara.hutool.core.map;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.thread.ConcurrencyTester;
import org.dromara.hutool.core.thread.ThreadUtil;
import org.dromara.hutool.core.util.ObjUtil;
@ -23,7 +24,7 @@ public class WeakConcurrentMapTest {
@Test
public void putAndGetTest(){
final WeakConcurrentMap<Object, Object> map = new WeakConcurrentMap<>();
final WeakKeyConcurrentMap<Object, Object> map = new WeakKeyConcurrentMap<>();
Object
key1 = new Object();
final Object value1 = new Object();
@ -57,7 +58,7 @@ public class WeakConcurrentMapTest {
@Test
public void getConcurrencyTest(){
final WeakConcurrentMap<String, String> cache = new WeakConcurrentMap<>();
final WeakKeyConcurrentMap<String, String> cache = new WeakKeyConcurrentMap<>();
final ConcurrencyTester tester = new ConcurrencyTester(2000);
tester.test(()-> cache.computeIfAbsent("aaa" + RandomUtil.randomInt(2), (key)-> "aaaValue"));

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.db.dialect;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.db.config.DbConfig;
import org.dromara.hutool.db.dialect.impl.*;

View File

@ -15,7 +15,7 @@ package org.dromara.hutool.db.ds;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.lang.Singleton;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.db.config.ConfigParser;
import org.dromara.hutool.db.config.DbConfig;

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.extra.spring.cglib;
import org.dromara.hutool.core.map.WeakConcurrentMap;
import org.dromara.hutool.core.map.reference.WeakKeyConcurrentMap;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.core.Converter;
@ -30,7 +30,7 @@ public enum BeanCopierCache {
*/
INSTANCE;
private final WeakConcurrentMap<String, BeanCopier> cache = new WeakConcurrentMap<>();
private final WeakKeyConcurrentMap<String, BeanCopier> cache = new WeakKeyConcurrentMap<>();
/**
* 获得类与转换器生成的key在{@link BeanCopier}的Map中对应的元素

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.http.html;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.text.CharUtil;
import java.util.ArrayList;

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.json.serialize;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.reflect.NullType;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.JSON;

View File

@ -12,7 +12,7 @@
package org.dromara.hutool.json.writer;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.reflect.NullType;
import org.dromara.hutool.core.util.ObjUtil;

View File

@ -24,7 +24,7 @@ import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.TableMap;
import org.dromara.hutool.core.map.multi.RowKeyTable;
import org.dromara.hutool.core.map.multi.Table;

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.setting;
import org.dromara.hutool.core.io.file.FileNameUtil;
import org.dromara.hutool.core.io.resource.NoResourceException;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.text.StrUtil;
import java.util.Map;

View File

@ -13,7 +13,7 @@
package org.dromara.hutool.setting.profile;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.setting.Setting;

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.setting.props;
import org.dromara.hutool.core.io.file.FileNameUtil;
import org.dromara.hutool.core.io.resource.NoResourceException;
import org.dromara.hutool.core.map.SafeConcurrentHashMap;
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
import org.dromara.hutool.core.text.StrUtil;
import java.util.Map;