Browse Source

Proper JCache metadata caching on non cache methods

Prior to this commit, the cache operation metadata cache was not
updated for a method not using the JCache annotations. This means
that every execution of said method was going through the process
of identifying if it was using the cache or not.

This commit adds a default placeholder identifying the absence of
metadata; this allows to flag such method as not having any metadata
at all.

Issue: SPR-12337
pull/661/merge
Stephane Nicoll 11 years ago
parent
commit
6f987a9cf8
  1. 22
      spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java

22
spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java vendored

@ -43,22 +43,34 @@ import org.springframework.util.ClassUtils;
public abstract class AbstractFallbackJCacheOperationSource public abstract class AbstractFallbackJCacheOperationSource
implements JCacheOperationSource { 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.
*/
private final static Object NULL_CACHING_ATTRIBUTE = new Object();
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private final Map<Object, JCacheOperation<?>> cache = private final Map<Object, Object> cache =
new ConcurrentHashMap<Object, JCacheOperation<?>>(1024); new ConcurrentHashMap<Object, Object>(1024);
@Override @Override
public JCacheOperation<?> getCacheOperation(Method method, Class<?> targetClass) { public JCacheOperation<?> getCacheOperation(Method method, Class<?> targetClass) {
// First, see if we have a cached value. // First, see if we have a cached value.
Object cacheKey = new MethodCacheKey(method, targetClass); Object cacheKey = new MethodCacheKey(method, targetClass);
JCacheOperation<?> cached = this.cache.get(cacheKey); Object cached = this.cache.get(cacheKey);
if (cached != null) { if (cached != null) {
return cached; if (cached == NULL_CACHING_ATTRIBUTE) {
return null;
}
return (JCacheOperation<?>) cached;
} }
else { else {
JCacheOperation<?> operation = computeCacheOperation(method, targetClass); JCacheOperation<?> operation = computeCacheOperation(method, targetClass);
if (operation != null) { if (operation == null) {
this.cache.put(cacheKey, NULL_CACHING_ATTRIBUTE);
}
else {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Adding cacheable method '" + method.getName() logger.debug("Adding cacheable method '" + method.getName()
+ "' with operation: " + operation); + "' with operation: " + operation);

Loading…
Cancel
Save