DATACMNS-1271 - Polishing.
Use ConcurrentHashMap in AnnotationBasedPersistentProperty for thread-safe annotation caching. Reintroduce eager cache check to prevent lambda instances on positive cache hits.
This commit is contained in:
committed by
Oliver Gierke
parent
78d21327bb
commit
7e7d4baa31
@@ -17,9 +17,9 @@ package org.springframework.data.mapping.model;
|
|||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
|
|||||||
private static final String SPRING_DATA_PACKAGE = "org.springframework.data";
|
private static final String SPRING_DATA_PACKAGE = "org.springframework.data";
|
||||||
|
|
||||||
private final @Nullable String value;
|
private final @Nullable String value;
|
||||||
private final Map<Class<? extends Annotation>, Optional<? extends Annotation>> annotationCache = new HashMap<>();
|
private final Map<Class<? extends Annotation>, Optional<? extends Annotation>> annotationCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private final Lazy<Boolean> usePropertyAccess = Lazy.of(() -> {
|
private final Lazy<Boolean> usePropertyAccess = Lazy.of(() -> {
|
||||||
|
|
||||||
@@ -230,6 +230,12 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private <A extends Annotation> Optional<A> doFindAnnotation(Class<A> annotationType) {
|
private <A extends Annotation> Optional<A> doFindAnnotation(Class<A> annotationType) {
|
||||||
|
|
||||||
|
Optional<? extends Annotation> annotation = annotationCache.get(annotationType);
|
||||||
|
|
||||||
|
if (annotation != null) {
|
||||||
|
return (Optional<A>) annotation;
|
||||||
|
}
|
||||||
|
|
||||||
return (Optional<A>) annotationCache.computeIfAbsent(annotationType, type -> {
|
return (Optional<A>) annotationCache.computeIfAbsent(annotationType, type -> {
|
||||||
|
|
||||||
return getAccessors() //
|
return getAccessors() //
|
||||||
|
|||||||
Reference in New Issue
Block a user