From a6d17a6bd77709ff623d94c8bce1192f4d7bd573 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 26 Aug 2015 09:56:09 +0200 Subject: [PATCH] Polishing --- ...AbstractFallbackJCacheOperationSource.java | 39 +++++++++---------- .../AbstractFallbackCacheOperationSource.java | 26 ++++++------- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java index b6e2c0c7c1b..c19e3a1e944 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -29,19 +29,18 @@ import org.springframework.core.BridgeMethodResolver; import org.springframework.util.ClassUtils; /** - * Abstract implementation of {@link JCacheOperationSource} that caches - * attributes for methods and implements a fallback policy: 1. specific - * target method; 2. declaring method. + * Abstract implementation of {@link JCacheOperationSource} that caches attributes + * for methods and implements a fallback policy: 1. specific target method; + * 2. declaring method. * - *

This implementation caches attributes by method after they are - * first used. + *

This implementation caches attributes by method after they are first used. * * @author Stephane Nicoll + * @author Juergen Hoeller * @since 4.1 * @see org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource */ -public abstract class AbstractFallbackJCacheOperationSource - implements JCacheOperationSource { +public abstract class AbstractFallbackJCacheOperationSource implements JCacheOperationSource { /** * Canonical value held in cache to indicate no caching attribute was @@ -49,34 +48,31 @@ public abstract class AbstractFallbackJCacheOperationSource */ private final static Object NULL_CACHING_ATTRIBUTE = new Object(); + protected final Log logger = LogFactory.getLog(getClass()); - private final Map cache = - new ConcurrentHashMap(1024); + private final Map cache = new ConcurrentHashMap(1024); + @Override public JCacheOperation getCacheOperation(Method method, Class targetClass) { - // First, see if we have a cached value. Object cacheKey = new MethodCacheKey(method, targetClass); Object cached = this.cache.get(cacheKey); + if (cached != null) { - if (cached == NULL_CACHING_ATTRIBUTE) { - return null; - } - return (JCacheOperation) cached; + return (cached != NULL_CACHING_ATTRIBUTE ? (JCacheOperation) cached : null); } else { JCacheOperation operation = computeCacheOperation(method, targetClass); - if (operation == null) { - this.cache.put(cacheKey, NULL_CACHING_ATTRIBUTE); - } - else { + if (operation != null) { if (logger.isDebugEnabled()) { - logger.debug("Adding cacheable method '" + method.getName() - + "' with operation: " + operation); + logger.debug("Adding cacheable method '" + method.getName() + "' with operation: " + operation); } this.cache.put(cacheKey, operation); } + else { + this.cache.put(cacheKey, NULL_CACHING_ATTRIBUTE); + } return operation; } } @@ -108,6 +104,7 @@ public abstract class AbstractFallbackJCacheOperationSource return null; } + /** * Subclasses need to implement this to return the caching operation * for the given method, if any. diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractFallbackCacheOperationSource.java b/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractFallbackCacheOperationSource.java index 222ee095914..d1bfc0724fa 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractFallbackCacheOperationSource.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractFallbackCacheOperationSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -59,6 +59,7 @@ public abstract class AbstractFallbackCacheOperationSource implements CacheOpera */ private final static Collection NULL_CACHING_ATTRIBUTE = Collections.emptyList(); + /** * Logger available to subclasses. *

As this base class is not marked Serializable, the logger will be recreated @@ -71,9 +72,10 @@ public abstract class AbstractFallbackCacheOperationSource implements CacheOpera *

As this base class is not marked Serializable, the cache will be recreated * after serialization - provided that the concrete subclass is Serializable. */ - final Map> attributeCache = + private final Map> attributeCache = new ConcurrentHashMap>(1024); + /** * Determine the caching attribute for this method invocation. *

Defaults to the class's caching attribute if no method attribute is found. @@ -84,30 +86,23 @@ public abstract class AbstractFallbackCacheOperationSource implements CacheOpera */ @Override public Collection getCacheOperations(Method method, Class targetClass) { - // First, see if we have a cached value. Object cacheKey = getCacheKey(method, targetClass); Collection cached = this.attributeCache.get(cacheKey); + if (cached != null) { - if (cached == NULL_CACHING_ATTRIBUTE) { - return null; - } - // Value will either be canonical value indicating there is no caching attribute, - // or an actual caching attribute. - return cached; + return (cached != NULL_CACHING_ATTRIBUTE ? cached : null); } else { - // We need to work it out. Collection cacheOps = computeCacheOperations(method, targetClass); - // Put it in the cache. - if (cacheOps == null) { - this.attributeCache.put(cacheKey, NULL_CACHING_ATTRIBUTE); - } - else { + if (cacheOps != null) { if (logger.isDebugEnabled()) { logger.debug("Adding cacheable method '" + method.getName() + "' with attribute: " + cacheOps); } this.attributeCache.put(cacheKey, cacheOps); } + else { + this.attributeCache.put(cacheKey, NULL_CACHING_ATTRIBUTE); + } return cacheOps; } } @@ -186,4 +181,5 @@ public abstract class AbstractFallbackCacheOperationSource implements CacheOpera protected boolean allowPublicMethodsOnly() { return false; } + }