From 903928e504b19411753d294e117b18e920a96fa2 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Wed, 18 May 2011 17:43:13 +0000 Subject: [PATCH] revised cache abstraction - removed AbstractDelegatingCache (a cache is not a map, no need to offer a template) + renamed ConcurrentCache to ConcurrentCacheMap git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4325 50f2f4bb-b051-0410-bef5-90022cba6387 --- org.springframework.context/.project | 52 +++++++------ .../cache/concurrent/ConcurrentCache.java | 57 --------------- .../ConcurrentCacheFactoryBean.java | 12 +-- .../ConcurrentMapCache.java} | 73 +++++++++---------- .../cache/concurrent/ConcurrentCacheTest.java | 2 +- 5 files changed, 72 insertions(+), 124 deletions(-) delete mode 100644 org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentCache.java rename org.springframework.context/src/main/java/org/springframework/cache/{support/AbstractDelegatingCache.java => concurrent/ConcurrentMapCache.java} (52%) diff --git a/org.springframework.context/.project b/org.springframework.context/.project index 4ca0e0f5c31..ab474368445 100644 --- a/org.springframework.context/.project +++ b/org.springframework.context/.project @@ -1,23 +1,29 @@ - - - org.springframework.context - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.springframework.ide.eclipse.core.springbuilder - - - - - - org.springframework.ide.eclipse.core.springnature - org.eclipse.jdt.core.javanature - - + + + org.springframework.context + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentCache.java b/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentCache.java deleted file mode 100644 index 13cdc057122..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentCache.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2010-2011 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 - * - * http://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.concurrent; - -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import org.springframework.cache.Cache; -import org.springframework.cache.support.AbstractDelegatingCache; - -/** - * Simple {@link Cache} implementation based on the JDK 1.5+ - * java.util.concurrent package. Useful for testing or simple caching scenarios. - * - * @author Costin Leau - */ -public class ConcurrentCache extends AbstractDelegatingCache { - - private final ConcurrentMap store; - private final String name; - - public ConcurrentCache() { - this(""); - } - - public ConcurrentCache(String name) { - this(new ConcurrentHashMap(), name); - } - - public ConcurrentCache(ConcurrentMap delegate, String name) { - super(delegate, true); - this.store = delegate; - this.name = name; - } - - public String getName() { - return name; - } - - public ConcurrentMap getNativeCache() { - return store; - } -} \ No newline at end of file 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/ConcurrentCacheFactoryBean.java index 5fce01a29b8..66bf95c42a6 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/ConcurrentCacheFactoryBean.java @@ -24,28 +24,28 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.util.StringUtils; /** - * Factory bean for easy configuration of {@link ConcurrentCache} through Spring. + * Factory bean for easy configuration of {@link ConcurrentMapCache} through Spring. * * @author Costin Leau */ -public class ConcurrentCacheFactoryBean implements FactoryBean>, BeanNameAware, +public class ConcurrentCacheFactoryBean implements FactoryBean>, BeanNameAware, InitializingBean { private String name = ""; - private ConcurrentCache cache; + private ConcurrentMapCache cache; private ConcurrentMap store; public void afterPropertiesSet() { - cache = (store == null ? new ConcurrentCache(name) : new ConcurrentCache(store, name)); + cache = (store == null ? new ConcurrentMapCache(name) : new ConcurrentMapCache(store, name, true)); } - public ConcurrentCache getObject() throws Exception { + public ConcurrentMapCache getObject() throws Exception { return cache; } public Class getObjectType() { - return (cache != null ? cache.getClass() : ConcurrentCache.class); + return (cache != null ? cache.getClass() : ConcurrentMapCache.class); } public boolean isSingleton() { diff --git a/org.springframework.context/src/main/java/org/springframework/cache/support/AbstractDelegatingCache.java b/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java similarity index 52% rename from org.springframework.context/src/main/java/org/springframework/cache/support/AbstractDelegatingCache.java rename to org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java index 9a041627b81..17341b67dc7 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/support/AbstractDelegatingCache.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java @@ -14,85 +14,84 @@ * limitations under the License. */ -package org.springframework.cache.support; +package org.springframework.cache.concurrent; import java.io.Serializable; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import org.springframework.cache.Cache; import org.springframework.cache.interceptor.DefaultValueWrapper; -import org.springframework.util.Assert; /** - * Abstract base class delegating most of the {@link Map}-like methods - * to the underlying cache. - * - * Note:Allows null values to be stored even (for cases where the - * underlying cache does not support them) as long as arbitrary serialized - * objects are supported. + * Simple {@link Cache} implementation based on the JDK 1.5+ + * java.util.concurrent package. Useful for testing or simple caching scenarios. + * + * Note:As {@link ConcurrentHashMap} (the default implementation used) does not allow null values to be stored + * this class will replace them with a predefined, internal object. This behaviour can be changed through the {@link #ConcurrentMapCache(ConcurrentMap, String, boolean)} + * constructor. * * @author Costin Leau */ -public abstract class AbstractDelegatingCache implements Cache { +public class ConcurrentMapCache implements Cache { private static class NullHolder implements Serializable { private static final long serialVersionUID = 1L; } - public static final Object NULL_HOLDER = new NullHolder(); - - private final Map delegate; + private static final Object NULL_HOLDER = new NullHolder(); + private final ConcurrentMap store; + private final String name; private final boolean allowNullValues; - /** - * Creates a new instance using the given delegate. - * - * @param map type - * @param delegate map delegate - */ - public > AbstractDelegatingCache(D delegate) { - this(delegate, false); + public ConcurrentMapCache() { + this(""); } - /** - * Creates a new instance using the given delegate. - * - * @param map type - * @param delegate map delegate - * @param allowNullValues flag indicating whether null values should be replaced or not - */ - public > AbstractDelegatingCache(D delegate, boolean allowNullValues) { - Assert.notNull(delegate); - this.delegate = delegate; + public ConcurrentMapCache(String name) { + this(new ConcurrentHashMap(), name, true); + } + + public ConcurrentMapCache(ConcurrentMap delegate, String name, boolean allowNullValues) { + this.store = delegate; + this.name = name; this.allowNullValues = allowNullValues; } + public String getName() { + return name; + } + public boolean getAllowNullValues() { return allowNullValues; } + public ConcurrentMap getNativeCache() { + return store; + } + public void clear() { - delegate.clear(); + store.clear(); } public ValueWrapper get(Object key) { - V v = delegate.get(key); + V v = store.get(key); return (v != null ? new DefaultValueWrapper(filterNull(v)) : null); } @SuppressWarnings("unchecked") public void put(K key, V value) { if (allowNullValues && value == null) { - Map map = delegate; + Map map = store; map.put(key, NULL_HOLDER); - } - else { - delegate.put(key, value); + } else { + store.put(key, value); } } public void evict(Object key) { - delegate.remove(key); + store.remove(key); } protected V filterNull(V val) { diff --git a/org.springframework.context/src/test/java/org/springframework/cache/concurrent/ConcurrentCacheTest.java b/org.springframework.context/src/test/java/org/springframework/cache/concurrent/ConcurrentCacheTest.java index c428da07310..ba22c8c0fe8 100644 --- a/org.springframework.context/src/test/java/org/springframework/cache/concurrent/ConcurrentCacheTest.java +++ b/org.springframework.context/src/test/java/org/springframework/cache/concurrent/ConcurrentCacheTest.java @@ -29,7 +29,7 @@ public class ConcurrentCacheTest extends AbstractNativeCacheTest nativeCache) { - return new ConcurrentCache(nativeCache, CACHE_NAME); + return new ConcurrentMapCache(nativeCache, CACHE_NAME, true); } @Override