Avoid storage of null marker per method for proxy decision purposes
Includes missing isCandidateClass support on JCacheOperationSource. Closes gh-20072
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,14 +27,13 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.core.MethodClassKey;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Abstract implementation of {@link JCacheOperationSource} that caches attributes
|
||||
* Abstract implementation of {@link JCacheOperationSource} that caches operations
|
||||
* for methods and implements a fallback policy: 1. specific target method;
|
||||
* 2. declaring method.
|
||||
*
|
||||
* <p>This implementation caches attributes by method after they are first used.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.1
|
||||
@@ -43,24 +42,39 @@ import org.springframework.lang.Nullable;
|
||||
public abstract class AbstractFallbackJCacheOperationSource implements JCacheOperationSource {
|
||||
|
||||
/**
|
||||
* Canonical value held in cache to indicate no caching attribute was
|
||||
* found for this method and we don't need to look again.
|
||||
* Canonical value held in cache to indicate no cache operation was
|
||||
* found for this method, and we don't need to look again.
|
||||
*/
|
||||
private static final Object NULL_CACHING_ATTRIBUTE = new Object();
|
||||
private static final Object NULL_CACHING_MARKER = new Object();
|
||||
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final Map<MethodClassKey, Object> cache = new ConcurrentHashMap<>(1024);
|
||||
private final Map<MethodClassKey, Object> operationCache = new ConcurrentHashMap<>(1024);
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasCacheOperation(Method method, @Nullable Class<?> targetClass) {
|
||||
return (getCacheOperation(method, targetClass, false) != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public JCacheOperation<?> getCacheOperation(Method method, @Nullable Class<?> targetClass) {
|
||||
return getCacheOperation(method, targetClass, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private JCacheOperation<?> getCacheOperation(Method method, @Nullable Class<?> targetClass, boolean cacheNull) {
|
||||
if (ReflectionUtils.isObjectMethod(method)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
MethodClassKey cacheKey = new MethodClassKey(method, targetClass);
|
||||
Object cached = this.cache.get(cacheKey);
|
||||
Object cached = this.operationCache.get(cacheKey);
|
||||
|
||||
if (cached != null) {
|
||||
return (cached != NULL_CACHING_ATTRIBUTE ? (JCacheOperation<?>) cached : null);
|
||||
return (cached != NULL_CACHING_MARKER ? (JCacheOperation<?>) cached : null);
|
||||
}
|
||||
else {
|
||||
JCacheOperation<?> operation = computeCacheOperation(method, targetClass);
|
||||
@@ -68,10 +82,10 @@ public abstract class AbstractFallbackJCacheOperationSource implements JCacheOpe
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Adding cacheable method '" + method.getName() + "' with operation: " + operation);
|
||||
}
|
||||
this.cache.put(cacheKey, operation);
|
||||
this.operationCache.put(cacheKey, operation);
|
||||
}
|
||||
else {
|
||||
this.cache.put(cacheKey, NULL_CACHING_ATTRIBUTE);
|
||||
else if (cacheNull) {
|
||||
this.operationCache.put(cacheKey, NULL_CACHING_MARKER);
|
||||
}
|
||||
return operation;
|
||||
}
|
||||
@@ -84,7 +98,7 @@ public abstract class AbstractFallbackJCacheOperationSource implements JCacheOpe
|
||||
return null;
|
||||
}
|
||||
|
||||
// The method may be on an interface, but we need attributes from the target class.
|
||||
// The method may be on an interface, but we need metadata from the target class.
|
||||
// If the target class is null, the method will be unchanged.
|
||||
Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,6 +20,7 @@ import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.cache.annotation.CacheDefaults;
|
||||
import javax.cache.annotation.CacheKeyGenerator;
|
||||
@@ -32,6 +33,7 @@ import javax.cache.annotation.CacheResult;
|
||||
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -41,10 +43,20 @@ import org.springframework.util.StringUtils;
|
||||
* {@link CacheRemoveAll} annotations.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.1
|
||||
*/
|
||||
public abstract class AnnotationJCacheOperationSource extends AbstractFallbackJCacheOperationSource {
|
||||
|
||||
private static final Set<Class<? extends Annotation>> JCACHE_OPERATION_ANNOTATIONS =
|
||||
Set.of(CacheResult.class, CachePut.class, CacheRemove.class, CacheRemoveAll.class);
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isCandidateClass(Class<?> targetClass) {
|
||||
return AnnotationUtils.isCandidateClass(targetClass, JCACHE_OPERATION_ANNOTATIONS);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JCacheOperation<?> findCacheOperation(Method method, @Nullable Class<?> targetType) {
|
||||
CacheResult cacheResult = method.getAnnotation(CacheResult.class);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,15 +16,9 @@
|
||||
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.ClassFilter;
|
||||
import org.springframework.aop.Pointcut;
|
||||
import org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor;
|
||||
import org.springframework.aop.support.StaticMethodMatcherPointcut;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Advisor driven by a {@link JCacheOperationSource}, used to include a
|
||||
@@ -46,6 +40,7 @@ public class BeanFactoryJCacheOperationSourceAdvisor extends AbstractBeanFactory
|
||||
* Set the cache operation attribute source which is used to find cache
|
||||
* attributes. This should usually be identical to the source reference
|
||||
* set on the cache interceptor itself.
|
||||
* @see JCacheInterceptor#setCacheOperationSource
|
||||
*/
|
||||
public void setCacheOperationSource(JCacheOperationSource cacheOperationSource) {
|
||||
this.pointcut.setCacheOperationSource(cacheOperationSource);
|
||||
@@ -64,37 +59,4 @@ public class BeanFactoryJCacheOperationSourceAdvisor extends AbstractBeanFactory
|
||||
return this.pointcut;
|
||||
}
|
||||
|
||||
|
||||
private static class JCacheOperationSourcePointcut extends StaticMethodMatcherPointcut implements Serializable {
|
||||
|
||||
@Nullable
|
||||
private JCacheOperationSource cacheOperationSource;
|
||||
|
||||
public void setCacheOperationSource(@Nullable JCacheOperationSource cacheOperationSource) {
|
||||
this.cacheOperationSource = cacheOperationSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Method method, Class<?> targetClass) {
|
||||
return (this.cacheOperationSource == null ||
|
||||
this.cacheOperationSource.getCacheOperation(method, targetClass) != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof JCacheOperationSourcePointcut that &&
|
||||
ObjectUtils.nullSafeEquals(this.cacheOperationSource, that.cacheOperationSource)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return JCacheOperationSourcePointcut.class.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getName() + ": " + this.cacheOperationSource;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,16 +25,48 @@ import org.springframework.lang.Nullable;
|
||||
* cache operation attributes from standard JSR-107 annotations.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.1
|
||||
* @see org.springframework.cache.interceptor.CacheOperationSource
|
||||
*/
|
||||
public interface JCacheOperationSource {
|
||||
|
||||
/**
|
||||
* Determine whether the given class is a candidate for cache operations
|
||||
* in the metadata format of this {@code JCacheOperationSource}.
|
||||
* <p>If this method returns {@code false}, the methods on the given class
|
||||
* will not get traversed for {@link #getCacheOperation} introspection.
|
||||
* Returning {@code false} is therefore an optimization for non-affected
|
||||
* classes, whereas {@code true} simply means that the class needs to get
|
||||
* fully introspected for each method on the given class individually.
|
||||
* @param targetClass the class to introspect
|
||||
* @return {@code false} if the class is known to have no cache operation
|
||||
* metadata at class or method level; {@code true} otherwise. The default
|
||||
* implementation returns {@code true}, leading to regular introspection.
|
||||
* @since 6.2
|
||||
* @see #hasCacheOperation
|
||||
*/
|
||||
default boolean isCandidateClass(Class<?> targetClass) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether there is a JSR-107 cache operation for the given method.
|
||||
* @param method the method to introspect
|
||||
* @param targetClass the target class (can be {@code null}, in which case
|
||||
* the declaring class of the method must be used)
|
||||
* @since 6.2
|
||||
* @see #getCacheOperation
|
||||
*/
|
||||
default boolean hasCacheOperation(Method method, @Nullable Class<?> targetClass) {
|
||||
return (getCacheOperation(method, targetClass) != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the cache operations for this method, or {@code null}
|
||||
* if the method contains no <em>JSR-107</em> related metadata.
|
||||
* @param method the method to introspect
|
||||
* @param targetClass the target class (may be {@code null}, in which case
|
||||
* @param targetClass the target class (can be {@code null}, in which case
|
||||
* the declaring class of the method must be used)
|
||||
* @return the cache operation for this method, or {@code null} if none found
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cache.jcache.interceptor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.ClassFilter;
|
||||
import org.springframework.aop.support.StaticMethodMatcherPointcut;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* A {@code Pointcut} that matches if the underlying {@link JCacheOperationSource}
|
||||
* has an operation for a given method.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 6.2
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
final class JCacheOperationSourcePointcut extends StaticMethodMatcherPointcut implements Serializable {
|
||||
|
||||
@Nullable
|
||||
private JCacheOperationSource cacheOperationSource;
|
||||
|
||||
|
||||
public JCacheOperationSourcePointcut() {
|
||||
setClassFilter(new JCacheOperationSourceClassFilter());
|
||||
}
|
||||
|
||||
|
||||
public void setCacheOperationSource(@Nullable JCacheOperationSource cacheOperationSource) {
|
||||
this.cacheOperationSource = cacheOperationSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Method method, Class<?> targetClass) {
|
||||
return (this.cacheOperationSource == null ||
|
||||
this.cacheOperationSource.hasCacheOperation(method, targetClass));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof JCacheOperationSourcePointcut that &&
|
||||
ObjectUtils.nullSafeEquals(this.cacheOperationSource, that.cacheOperationSource)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return JCacheOperationSourcePointcut.class.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getName() + ": " + this.cacheOperationSource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@link ClassFilter} that delegates to {@link JCacheOperationSource#isCandidateClass}
|
||||
* for filtering classes whose methods are not worth searching to begin with.
|
||||
*/
|
||||
private final class JCacheOperationSourceClassFilter implements ClassFilter {
|
||||
|
||||
@Override
|
||||
public boolean matches(Class<?> clazz) {
|
||||
if (CacheManager.class.isAssignableFrom(clazz)) {
|
||||
return false;
|
||||
}
|
||||
return (cacheOperationSource == null || cacheOperationSource.isCandidateClass(clazz));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private JCacheOperationSource getCacheOperationSource() {
|
||||
return cacheOperationSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof JCacheOperationSourceClassFilter that &&
|
||||
ObjectUtils.nullSafeEquals(getCacheOperationSource(), that.getCacheOperationSource())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return JCacheOperationSourceClassFilter.class.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JCacheOperationSourceClassFilter.class.getName() + ": " + getCacheOperationSource();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user