Skip to content

Commit b39673a

Browse files
author
Costin Leau
committed
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
1 parent dadd0f5 commit b39673a

File tree

19 files changed

+63
-67
lines changed

19 files changed

+63
-67
lines changed

org.springframework.aspects/src/test/java/org/springframework/cache/config/annotation-cache-aspectj.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
2020
<property name="caches">
2121
<set>
22-
<bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="default"/>
22+
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
2323
</set>
2424
</property>
2525
</bean>

org.springframework.context/src/main/java/org/springframework/cache/Cache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@
2626
*
2727
* @author Costin Leau
2828
*/
29-
public interface Cache<K, V> {
29+
public interface Cache {
3030

3131
/**
3232
* A (wrapper) object representing a cache value.
3333
*/
34-
interface ValueWrapper<V> {
34+
interface ValueWrapper {
3535
/**
3636
* Returns the actual value in the cache.
3737
*
3838
* @return cache value
3939
*/
40-
V get();
40+
Object get();
4141
}
4242

4343
/**
@@ -62,7 +62,7 @@ interface ValueWrapper<V> {
6262
* @return the value to which this cache maps the specified key, or
6363
* <tt>null</tt> if the cache contains no mapping for this key.
6464
*/
65-
ValueWrapper<V> get(Object key);
65+
ValueWrapper get(Object key);
6666

6767
/**
6868
* Associates the specified value with the specified key in this cache.
@@ -72,7 +72,7 @@ interface ValueWrapper<V> {
7272
* @param key key with which the specified value is to be associated.
7373
* @param value value to be associated with the specified key.
7474
*/
75-
void put(K key, V value);
75+
void put(Object key, Object value);
7676

7777
/**
7878
* Evicts the mapping for this key from this cache if it is present.

org.springframework.context/src/main/java/org/springframework/cache/CacheManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface CacheManager {
3232
* @param name cache identifier - cannot be null
3333
* @return associated cache or null if none is found
3434
*/
35-
<K, V> Cache<K, V> getCache(String name);
35+
Cache getCache(String name);
3636

3737
/**
3838
* Returns a collection of the caches known by this cache manager.

org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
*
3535
* @author Costin Leau
3636
*/
37-
public class ConcurrentMapCache<K, V> implements Cache<K, V> {
37+
public class ConcurrentMapCache implements Cache {
3838

3939
private static class NullHolder implements Serializable {
4040
private static final long serialVersionUID = 1L;
4141
}
4242

4343
private static final Object NULL_HOLDER = new NullHolder();
44-
private final ConcurrentMap<K, V> store;
44+
private final ConcurrentMap store;
4545
private final String name;
4646
private final boolean allowNullValues;
4747

@@ -50,10 +50,10 @@ public ConcurrentMapCache() {
5050
}
5151

5252
public ConcurrentMapCache(String name) {
53-
this(new ConcurrentHashMap<K, V>(), name, true);
53+
this(new ConcurrentHashMap(), name, true);
5454
}
5555

56-
public ConcurrentMapCache(ConcurrentMap<K, V> delegate, String name, boolean allowNullValues) {
56+
public ConcurrentMapCache(ConcurrentMap delegate, String name, boolean allowNullValues) {
5757
this.store = delegate;
5858
this.name = name;
5959
this.allowNullValues = allowNullValues;
@@ -67,21 +67,20 @@ public boolean getAllowNullValues() {
6767
return allowNullValues;
6868
}
6969

70-
public ConcurrentMap<K, V> getNativeCache() {
70+
public ConcurrentMap getNativeCache() {
7171
return store;
7272
}
7373

7474
public void clear() {
7575
store.clear();
7676
}
7777

78-
public ValueWrapper<V> get(Object key) {
79-
V v = store.get(key);
80-
return (v != null ? new DefaultValueWrapper<V>(filterNull(v)) : null);
78+
public ValueWrapper get(Object key) {
79+
Object v = store.get(key);
80+
return (v != null ? new DefaultValueWrapper(filterNull(v)) : null);
8181
}
8282

83-
@SuppressWarnings("unchecked")
84-
public void put(K key, V value) {
83+
public void put(Object key, Object value) {
8584
if (allowNullValues && value == null) {
8685
Map map = store;
8786
map.put(key, NULL_HOLDER);
@@ -94,7 +93,7 @@ public void evict(Object key) {
9493
store.remove(key);
9594
}
9695

97-
protected V filterNull(V val) {
96+
protected Object filterNull(Object val) {
9897
if (allowNullValues && val == NULL_HOLDER) {
9998
return null;
10099
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@
2828
*
2929
* @author Costin Leau
3030
*/
31-
public class ConcurrentCacheFactoryBean<K, V> implements FactoryBean<ConcurrentMapCache<K, V>>, BeanNameAware,
31+
public class ConcurrentMapCacheFactoryBean implements FactoryBean<ConcurrentMapCache>, BeanNameAware,
3232
InitializingBean {
3333

3434
private String name = "";
35-
private ConcurrentMapCache<K, V> cache;
35+
private ConcurrentMapCache cache;
3636

37-
private ConcurrentMap<K, V> store;
37+
private ConcurrentMap store;
3838

3939
public void afterPropertiesSet() {
40-
cache = (store == null ? new ConcurrentMapCache<K, V>(name) : new ConcurrentMapCache<K, V>(store, name, true));
40+
cache = (store == null ? new ConcurrentMapCache(name) : new ConcurrentMapCache(store, name, true));
4141
}
4242

43-
public ConcurrentMapCache<K, V> getObject() throws Exception {
43+
public ConcurrentMapCache getObject() throws Exception {
4444
return cache;
4545
}
4646

@@ -62,7 +62,7 @@ public void setName(String name) {
6262
this.name = name;
6363
}
6464

65-
public void setStore(ConcurrentMap<K, V> store) {
65+
public void setStore(ConcurrentMap store) {
6666
this.store = store;
6767
}
6868
}

org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* @author Costin Leau
3131
*/
32-
public class EhCacheCache implements Cache<Object, Object> {
32+
public class EhCacheCache implements Cache {
3333

3434
private final Ehcache cache;
3535

@@ -58,7 +58,7 @@ public void clear() {
5858
cache.removeAll();
5959
}
6060

61-
public ValueWrapper<Object> get(Object key) {
61+
public ValueWrapper get(Object key) {
6262
Element element = cache.get(key);
6363
return (element != null ? new DefaultValueWrapper<Object>(element.getObjectValue()) : null);
6464
}

org.springframework.context/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public class EhCacheCacheManager extends AbstractCacheManager {
3636
private net.sf.ehcache.CacheManager cacheManager;
3737

3838
@Override
39-
protected Collection<Cache<?, ?>> loadCaches() {
39+
protected Collection<Cache> loadCaches() {
4040
Assert.notNull(cacheManager, "a backing Ehcache cache manager is required");
4141
Status status = cacheManager.getStatus();
4242

4343
Assert.isTrue(Status.STATUS_ALIVE.equals(status),
4444
"an 'alive' Ehcache cache manager is required - current cache is " + status.toString());
4545

4646
String[] names = cacheManager.getCacheNames();
47-
Collection<Cache<?, ?>> caches = new LinkedHashSet<Cache<?, ?>>(names.length);
47+
Collection<Cache> caches = new LinkedHashSet<Cache>(names.length);
4848

4949
for (String name : names) {
5050
caches.add(new EhCacheCache(cacheManager.getEhcache(name)));
@@ -53,8 +53,7 @@ public class EhCacheCacheManager extends AbstractCacheManager {
5353
return caches;
5454
}
5555

56-
@SuppressWarnings("unchecked")
57-
public <K, V> Cache<K, V> getCache(String name) {
56+
public Cache getCache(String name) {
5857
Cache cache = super.getCache(name);
5958
if (cache == null) {
6059
// check the Ehcache cache again

org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ public void setCacheDefinitionSources(CacheOperationSource... cacheDefinitionSou
124124
cacheDefinitionSources) : cacheDefinitionSources[0]);
125125
}
126126

127-
protected Collection<Cache<?, ?>> getCaches(CacheOperation operation) {
127+
protected Collection<Cache> getCaches(CacheOperation operation) {
128128
Set<String> cacheNames = operation.getCacheNames();
129129

130-
Collection<Cache<?, ?>> caches = new ArrayList<Cache<?, ?>>(cacheNames.size());
130+
Collection<Cache> caches = new ArrayList<Cache>(cacheNames.size());
131131

132132
for (String cacheName : cacheNames) {
133-
Cache<Object, Object> cache = cacheManager.getCache(cacheName);
133+
Cache cache = cacheManager.getCache(cacheName);
134134
if (cache == null) {
135135
throw new IllegalArgumentException("Cannot find cache named [" + cacheName + "] for " + operation);
136136
}
@@ -145,7 +145,6 @@ protected CacheOperationContext getOperationContext(CacheOperation operation, Me
145145
return new CacheOperationContext(operation, method, args, target, targetClass);
146146
}
147147

148-
@SuppressWarnings("unchecked")
149148
protected Object execute(Callable<Object> invocation, Object target, Method method, Object[] args) throws Exception {
150149
// get backing class
151150
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(target);
@@ -163,7 +162,7 @@ protected Object execute(Callable<Object> invocation, Object target, Method meth
163162
// analyze caching information
164163
if (cacheOp != null) {
165164
CacheOperationContext context = getOperationContext(cacheOp, method, args, target, targetClass);
166-
Collection<Cache<?, ?>> caches = context.getCaches();
165+
Collection<Cache> caches = context.getCaches();
167166

168167
if (context.hasConditionPassed()) {
169168
// check operation
@@ -183,9 +182,9 @@ protected Object execute(Callable<Object> invocation, Object target, Method meth
183182
// for each cache
184183
boolean cacheHit = false;
185184

186-
for (Iterator<Cache<?, ?>> iterator = caches.iterator(); iterator.hasNext() && !cacheHit;) {
185+
for (Iterator<Cache> iterator = caches.iterator(); iterator.hasNext() && !cacheHit;) {
187186
Cache cache = iterator.next();
188-
Cache.ValueWrapper<Object> wrapper = cache.get(key);
187+
Cache.ValueWrapper wrapper = cache.get(key);
189188

190189
if (wrapper != null) {
191190
cacheHit = true;
@@ -255,7 +254,7 @@ protected Object execute(Callable<Object> invocation, Object target, Method meth
255254
protected class CacheOperationContext {
256255

257256
private CacheOperation operation;
258-
private final Collection<Cache<?, ?>> caches;
257+
private final Collection<Cache> caches;
259258
private final Object target;
260259
private final Method method;
261260
private final Object[] args;
@@ -307,7 +306,7 @@ protected Object generateKey() {
307306
return keyGenerator.extract(target, method, args);
308307
}
309308

310-
protected Collection<Cache<?, ?>> getCaches() {
309+
protected Collection<Cache> getCaches() {
311310
return caches;
312311
}
313312
}

org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheExpressionRootObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,5 @@ interface CacheExpressionRootObject {
6868
*
6969
* @return current cache
7070
*/
71-
Collection<Cache<?,?>> getCaches();
71+
Collection<Cache> getCaches();
7272
}

org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultCacheExpressionRootObject.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public class DefaultCacheExpressionRootObject implements CacheExpressionRootObje
3333
private final Class<?> targetClass;
3434
private final String methodName;
3535
private final Method method;
36-
private final Collection<Cache<?, ?>> caches;
36+
private final Collection<Cache> caches;
3737
private final Object[] args;
3838

39-
public DefaultCacheExpressionRootObject(Collection<Cache<?, ?>> caches, Method method, Object[] args,
39+
public DefaultCacheExpressionRootObject(Collection<Cache> caches, Method method, Object[] args,
4040
Object target, Class<?> targetClass) {
4141
Assert.notNull(method, "method is required");
4242
Assert.notNull(targetClass, "targetClass is required");
@@ -52,7 +52,7 @@ public String getMethodName() {
5252
return methodName;
5353
}
5454

55-
public Collection<Cache<?, ?>> getCaches() {
55+
public Collection<Cache> getCaches() {
5656
return caches;
5757
}
5858

0 commit comments

Comments
 (0)