From 7a91725ba6efb6187087e96d5781d0a34d9b39ab Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Wed, 18 May 2011 18:34:41 +0000 Subject: [PATCH] revised cache abstraction - removed generics from Cache/CacheManager (they add no value since it's an SPI not API) + update docs and tests + renamed ConcurrentCacheFactoryBean to ConcurrentMapCacheFactoryBean git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4326 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../cache/config/annotation-cache-aspectj.xml | 2 +- .../java/org/springframework/cache/Cache.java | 10 ++++----- .../springframework/cache/CacheManager.java | 2 +- .../cache/concurrent/ConcurrentMapCache.java | 21 +++++++++---------- ...ava => ConcurrentMapCacheFactoryBean.java} | 12 +++++------ .../cache/ehcache/EhCacheCache.java | 4 ++-- .../cache/ehcache/EhCacheCacheManager.java | 7 +++---- .../cache/interceptor/CacheAspectSupport.java | 17 +++++++-------- .../CacheExpressionRootObject.java | 2 +- .../DefaultCacheExpressionRootObject.java | 6 +++--- .../interceptor/DefaultValueWrapper.java | 8 +++---- .../interceptor/ExpressionEvaluator.java | 2 +- .../cache/support/AbstractCacheManager.java | 15 +++++++------ .../cache/support/CompositeCacheManager.java | 4 ++-- .../cache/support/SimpleCacheManager.java | 6 +++--- .../cache/config/AbstractAnnotationTest.java | 4 ++-- .../config/annotationDrivenCacheConfig.xml | 2 +- .../config/annotationDrivenCacheNamespace.xml | 2 +- spring-framework-reference/src/cache.xml | 4 ++-- 19 files changed, 63 insertions(+), 67 deletions(-) rename org.springframework.context/src/main/java/org/springframework/cache/concurrent/{ConcurrentCacheFactoryBean.java => ConcurrentMapCacheFactoryBean.java} (75%) diff --git a/org.springframework.aspects/src/test/java/org/springframework/cache/config/annotation-cache-aspectj.xml b/org.springframework.aspects/src/test/java/org/springframework/cache/config/annotation-cache-aspectj.xml index 741cf69e3a5..da2514ccd53 100644 --- a/org.springframework.aspects/src/test/java/org/springframework/cache/config/annotation-cache-aspectj.xml +++ b/org.springframework.aspects/src/test/java/org/springframework/cache/config/annotation-cache-aspectj.xml @@ -19,7 +19,7 @@ - + diff --git a/org.springframework.context/src/main/java/org/springframework/cache/Cache.java b/org.springframework.context/src/main/java/org/springframework/cache/Cache.java index aab14aa7ad6..b37030dbf0a 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/Cache.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/Cache.java @@ -26,18 +26,18 @@ package org.springframework.cache; * * @author Costin Leau */ -public interface Cache { +public interface Cache { /** * A (wrapper) object representing a cache value. */ - interface ValueWrapper { + interface ValueWrapper { /** * Returns the actual value in the cache. * * @return cache value */ - V get(); + Object get(); } /** @@ -62,7 +62,7 @@ public interface Cache { * @return the value to which this cache maps the specified key, or * null if the cache contains no mapping for this key. */ - ValueWrapper get(Object key); + ValueWrapper get(Object key); /** * Associates the specified value with the specified key in this cache. @@ -72,7 +72,7 @@ public interface Cache { * @param key key with which the specified value is to be associated. * @param value value to be associated with the specified key. */ - void put(K key, V value); + void put(Object key, Object value); /** * Evicts the mapping for this key from this cache if it is present. diff --git a/org.springframework.context/src/main/java/org/springframework/cache/CacheManager.java b/org.springframework.context/src/main/java/org/springframework/cache/CacheManager.java index cdf6b960cbf..268e9c3a409 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/CacheManager.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/CacheManager.java @@ -32,7 +32,7 @@ public interface CacheManager { * @param name cache identifier - cannot be null * @return associated cache or null if none is found */ - Cache getCache(String name); + Cache getCache(String name); /** * Returns a collection of the caches known by this cache manager. diff --git a/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java b/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java index 17341b67dc7..12468e878f8 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java @@ -34,14 +34,14 @@ import org.springframework.cache.interceptor.DefaultValueWrapper; * * @author Costin Leau */ -public class ConcurrentMapCache implements Cache { +public class ConcurrentMapCache implements Cache { private static class NullHolder implements Serializable { private static final long serialVersionUID = 1L; } private static final Object NULL_HOLDER = new NullHolder(); - private final ConcurrentMap store; + private final ConcurrentMap store; private final String name; private final boolean allowNullValues; @@ -50,10 +50,10 @@ public class ConcurrentMapCache implements Cache { } public ConcurrentMapCache(String name) { - this(new ConcurrentHashMap(), name, true); + this(new ConcurrentHashMap(), name, true); } - public ConcurrentMapCache(ConcurrentMap delegate, String name, boolean allowNullValues) { + public ConcurrentMapCache(ConcurrentMap delegate, String name, boolean allowNullValues) { this.store = delegate; this.name = name; this.allowNullValues = allowNullValues; @@ -67,7 +67,7 @@ public class ConcurrentMapCache implements Cache { return allowNullValues; } - public ConcurrentMap getNativeCache() { + public ConcurrentMap getNativeCache() { return store; } @@ -75,13 +75,12 @@ public class ConcurrentMapCache implements Cache { store.clear(); } - public ValueWrapper get(Object key) { - V v = store.get(key); - return (v != null ? new DefaultValueWrapper(filterNull(v)) : null); + public ValueWrapper get(Object key) { + Object v = store.get(key); + return (v != null ? new DefaultValueWrapper(filterNull(v)) : null); } - @SuppressWarnings("unchecked") - public void put(K key, V value) { + public void put(Object key, Object value) { if (allowNullValues && value == null) { Map map = store; map.put(key, NULL_HOLDER); @@ -94,7 +93,7 @@ public class ConcurrentMapCache implements Cache { store.remove(key); } - protected V filterNull(V val) { + protected Object filterNull(Object val) { if (allowNullValues && val == NULL_HOLDER) { return null; } diff --git a/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentCacheFactoryBean.java b/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheFactoryBean.java similarity index 75% rename from org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentCacheFactoryBean.java rename to org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheFactoryBean.java index 66bf95c42a6..6e6a501dd6e 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentCacheFactoryBean.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheFactoryBean.java @@ -28,19 +28,19 @@ import org.springframework.util.StringUtils; * * @author Costin Leau */ -public class ConcurrentCacheFactoryBean implements FactoryBean>, BeanNameAware, +public class ConcurrentMapCacheFactoryBean implements FactoryBean, BeanNameAware, InitializingBean { private String name = ""; - private ConcurrentMapCache cache; + private ConcurrentMapCache cache; - private ConcurrentMap store; + private ConcurrentMap store; public void afterPropertiesSet() { - cache = (store == null ? new ConcurrentMapCache(name) : new ConcurrentMapCache(store, name, true)); + cache = (store == null ? new ConcurrentMapCache(name) : new ConcurrentMapCache(store, name, true)); } - public ConcurrentMapCache getObject() throws Exception { + public ConcurrentMapCache getObject() throws Exception { return cache; } @@ -62,7 +62,7 @@ public class ConcurrentCacheFactoryBean implements FactoryBean store) { + public void setStore(ConcurrentMap store) { this.store = store; } } \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java b/org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java index 4ec6e609ba2..8845ff01870 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java @@ -29,7 +29,7 @@ import org.springframework.util.Assert; * * @author Costin Leau */ -public class EhCacheCache implements Cache { +public class EhCacheCache implements Cache { private final Ehcache cache; @@ -58,7 +58,7 @@ public class EhCacheCache implements Cache { cache.removeAll(); } - public ValueWrapper get(Object key) { + public ValueWrapper get(Object key) { Element element = cache.get(key); return (element != null ? new DefaultValueWrapper(element.getObjectValue()) : null); } diff --git a/org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java b/org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java index bdbd4246952..a623f918d54 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java @@ -36,7 +36,7 @@ public class EhCacheCacheManager extends AbstractCacheManager { private net.sf.ehcache.CacheManager cacheManager; @Override - protected Collection> loadCaches() { + protected Collection loadCaches() { Assert.notNull(cacheManager, "a backing Ehcache cache manager is required"); Status status = cacheManager.getStatus(); @@ -44,7 +44,7 @@ public class EhCacheCacheManager extends AbstractCacheManager { "an 'alive' Ehcache cache manager is required - current cache is " + status.toString()); String[] names = cacheManager.getCacheNames(); - Collection> caches = new LinkedHashSet>(names.length); + Collection caches = new LinkedHashSet(names.length); for (String name : names) { caches.add(new EhCacheCache(cacheManager.getEhcache(name))); @@ -53,8 +53,7 @@ public class EhCacheCacheManager extends AbstractCacheManager { return caches; } - @SuppressWarnings("unchecked") - public Cache getCache(String name) { + public Cache getCache(String name) { Cache cache = super.getCache(name); if (cache == null) { // check the Ehcache cache again diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java index 4b82f7945c6..093ae72e07f 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java @@ -124,13 +124,13 @@ public abstract class CacheAspectSupport implements InitializingBean { cacheDefinitionSources) : cacheDefinitionSources[0]); } - protected Collection> getCaches(CacheOperation operation) { + protected Collection getCaches(CacheOperation operation) { Set cacheNames = operation.getCacheNames(); - Collection> caches = new ArrayList>(cacheNames.size()); + Collection caches = new ArrayList(cacheNames.size()); for (String cacheName : cacheNames) { - Cache cache = cacheManager.getCache(cacheName); + Cache cache = cacheManager.getCache(cacheName); if (cache == null) { throw new IllegalArgumentException("Cannot find cache named [" + cacheName + "] for " + operation); } @@ -145,7 +145,6 @@ public abstract class CacheAspectSupport implements InitializingBean { return new CacheOperationContext(operation, method, args, target, targetClass); } - @SuppressWarnings("unchecked") protected Object execute(Callable invocation, Object target, Method method, Object[] args) throws Exception { // get backing class Class targetClass = AopProxyUtils.ultimateTargetClass(target); @@ -163,7 +162,7 @@ public abstract class CacheAspectSupport implements InitializingBean { // analyze caching information if (cacheOp != null) { CacheOperationContext context = getOperationContext(cacheOp, method, args, target, targetClass); - Collection> caches = context.getCaches(); + Collection caches = context.getCaches(); if (context.hasConditionPassed()) { // check operation @@ -183,9 +182,9 @@ public abstract class CacheAspectSupport implements InitializingBean { // for each cache boolean cacheHit = false; - for (Iterator> iterator = caches.iterator(); iterator.hasNext() && !cacheHit;) { + for (Iterator iterator = caches.iterator(); iterator.hasNext() && !cacheHit;) { Cache cache = iterator.next(); - Cache.ValueWrapper wrapper = cache.get(key); + Cache.ValueWrapper wrapper = cache.get(key); if (wrapper != null) { cacheHit = true; @@ -255,7 +254,7 @@ public abstract class CacheAspectSupport implements InitializingBean { protected class CacheOperationContext { private CacheOperation operation; - private final Collection> caches; + private final Collection caches; private final Object target; private final Method method; private final Object[] args; @@ -307,7 +306,7 @@ public abstract class CacheAspectSupport implements InitializingBean { return keyGenerator.extract(target, method, args); } - protected Collection> getCaches() { + protected Collection getCaches() { return caches; } } diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheExpressionRootObject.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheExpressionRootObject.java index 9bdaf97c29f..ba81aa927ee 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheExpressionRootObject.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheExpressionRootObject.java @@ -68,5 +68,5 @@ interface CacheExpressionRootObject { * * @return current cache */ - Collection> getCaches(); + Collection getCaches(); } diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultCacheExpressionRootObject.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultCacheExpressionRootObject.java index 6262604c624..e668bc964fd 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultCacheExpressionRootObject.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultCacheExpressionRootObject.java @@ -33,10 +33,10 @@ public class DefaultCacheExpressionRootObject implements CacheExpressionRootObje private final Class targetClass; private final String methodName; private final Method method; - private final Collection> caches; + private final Collection caches; private final Object[] args; - public DefaultCacheExpressionRootObject(Collection> caches, Method method, Object[] args, + public DefaultCacheExpressionRootObject(Collection caches, Method method, Object[] args, Object target, Class targetClass) { Assert.notNull(method, "method is required"); Assert.notNull(targetClass, "targetClass is required"); @@ -52,7 +52,7 @@ public class DefaultCacheExpressionRootObject implements CacheExpressionRootObje return methodName; } - public Collection> getCaches() { + public Collection getCaches() { return caches; } diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultValueWrapper.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultValueWrapper.java index fe244c0a00f..cbe2780cb38 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultValueWrapper.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultValueWrapper.java @@ -23,15 +23,15 @@ import org.springframework.cache.Cache.ValueWrapper; * * @author Costin Leau */ -public class DefaultValueWrapper implements ValueWrapper { +public class DefaultValueWrapper implements ValueWrapper { - private final V value; + private final Object value; - public DefaultValueWrapper(V value) { + public DefaultValueWrapper(Object value) { this.value = value; } - public V get() { + public Object get() { return value; } } diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/ExpressionEvaluator.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/ExpressionEvaluator.java index 3d216b519d4..47e95a7dd68 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/ExpressionEvaluator.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/ExpressionEvaluator.java @@ -47,7 +47,7 @@ class ExpressionEvaluator { private Map keyCache = new ConcurrentHashMap(); private Map targetMethodCache = new ConcurrentHashMap(); - EvaluationContext createEvaluationContext(Collection> caches, Method method, Object[] args, + EvaluationContext createEvaluationContext(Collection caches, Method method, Object[] args, Object target, Class targetClass) { DefaultCacheExpressionRootObject rootObject = new DefaultCacheExpressionRootObject(caches, method, args, target, targetClass); diff --git a/org.springframework.context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java b/org.springframework.context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java index e81027dbf39..3e393dd8085 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java @@ -37,11 +37,11 @@ import org.springframework.util.Assert; public abstract class AbstractCacheManager implements CacheManager, InitializingBean { // fast lookup by name map - private final ConcurrentMap> caches = new ConcurrentHashMap>(); + private final ConcurrentMap caches = new ConcurrentHashMap(); private Collection names; public void afterPropertiesSet() { - Collection> cacheSet = loadCaches(); + Collection cacheSet = loadCaches(); Assert.notEmpty(cacheSet); @@ -50,7 +50,7 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing // preserve the initial order of the cache names Set cacheNames = new LinkedHashSet(cacheSet.size()); - for (Cache cache : cacheSet) { + for (Cache cache : cacheSet) { caches.put(cache.getName(), cache); cacheNames.add(cache.getName()); } @@ -64,20 +64,19 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing * * @param caches the collection of caches handled by the manager */ - protected abstract Collection> loadCaches(); + protected abstract Collection loadCaches(); /** * Returns the internal cache map. * * @return internal cache map */ - protected final ConcurrentMap> getCacheMap() { + protected final ConcurrentMap getCacheMap() { return caches; } - @SuppressWarnings("unchecked") - public Cache getCache(String name) { - return (Cache) caches.get(name); + public Cache getCache(String name) { + return caches.get(name); } public Collection getCacheNames() { diff --git a/org.springframework.context/src/main/java/org/springframework/cache/support/CompositeCacheManager.java b/org.springframework.context/src/main/java/org/springframework/cache/support/CompositeCacheManager.java index c882558e049..00dee1fa90f 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/support/CompositeCacheManager.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/support/CompositeCacheManager.java @@ -35,8 +35,8 @@ public class CompositeCacheManager implements CacheManager { private CacheManager[] cacheManagers; - public Cache getCache(String name) { - Cache cache = null; + public Cache getCache(String name) { + Cache cache = null; for (CacheManager cacheManager : cacheManagers) { cache = cacheManager.getCache(name); if (cache != null) { diff --git a/org.springframework.context/src/main/java/org/springframework/cache/support/SimpleCacheManager.java b/org.springframework.context/src/main/java/org/springframework/cache/support/SimpleCacheManager.java index 8c31b79c03c..3b910fa6fb7 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/support/SimpleCacheManager.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/support/SimpleCacheManager.java @@ -28,14 +28,14 @@ import org.springframework.cache.Cache; */ public class SimpleCacheManager extends AbstractCacheManager { - private Collection> caches; + private Collection caches; @Override - protected Collection> loadCaches() { + protected Collection loadCaches() { return caches; } - public void setCaches(Collection> caches) { + public void setCaches(Collection caches) { this.caches = caches; } } diff --git a/org.springframework.context/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java b/org.springframework.context/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java index 95ce7dab09b..6d5ba97a625 100644 --- a/org.springframework.context/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java +++ b/org.springframework.context/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java @@ -133,7 +133,7 @@ public abstract class AbstractAnnotationTest { Object key = new Object(); Object r1 = service.name(key); assertSame(r1, service.name(key)); - Cache cache = cm.getCache("default"); + Cache cache = cm.getCache("default"); // assert the method name is used assertNotNull(cache.get(keyName)); } @@ -142,7 +142,7 @@ public abstract class AbstractAnnotationTest { Object key = new Object(); Object r1 = service.rootVars(key); assertSame(r1, service.rootVars(key)); - Cache cache = cm.getCache("default"); + Cache cache = cm.getCache("default"); // assert the method name is used String expectedKey = "rootVarsrootVars" + AopProxyUtils.ultimateTargetClass(service) + service; assertNotNull(cache.get(expectedKey)); diff --git a/org.springframework.context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheConfig.xml b/org.springframework.context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheConfig.xml index daa81d5a5b8..167f778fe62 100644 --- a/org.springframework.context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheConfig.xml +++ b/org.springframework.context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheConfig.xml @@ -29,7 +29,7 @@ - + diff --git a/org.springframework.context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheNamespace.xml b/org.springframework.context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheNamespace.xml index f296fdfddda..b50554e842d 100644 --- a/org.springframework.context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheNamespace.xml +++ b/org.springframework.context/src/test/resources/org/springframework/cache/config/annotationDrivenCacheNamespace.xml @@ -16,7 +16,7 @@ - + diff --git a/spring-framework-reference/src/cache.xml b/spring-framework-reference/src/cache.xml index f86b2c61c8b..55e3bc8233a 100644 --- a/spring-framework-reference/src/cache.xml +++ b/spring-framework-reference/src/cache.xml @@ -400,8 +400,8 @@ public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)]]>< - - + + ]]>