diff --git a/source/java/org/alfresco/repo/cache/DefaultSimpleCache.java b/source/java/org/alfresco/repo/cache/DefaultSimpleCache.java index 37349a238c..c224ff1cdc 100644 --- a/source/java/org/alfresco/repo/cache/DefaultSimpleCache.java +++ b/source/java/org/alfresco/repo/cache/DefaultSimpleCache.java @@ -20,13 +20,9 @@ package org.alfresco.repo.cache; import java.io.Serializable; import java.util.AbstractMap; -import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; import org.springframework.beans.factory.BeanNameAware; -import org.springframework.beans.factory.InitializingBean; import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; import com.googlecode.concurrentlinkedhashmap.Weighers; @@ -37,34 +33,43 @@ import com.googlecode.concurrentlinkedhashmap.Weighers; * @author Matt Ward */ public final class DefaultSimpleCache - implements SimpleCache, BeanNameAware, InitializingBean + implements SimpleCache, BeanNameAware { - private Map> map; - private int maxItems = 1000000; + private static final int DEFAULT_CAPACITY = 200000; + private ConcurrentLinkedHashMap> map; private String cacheName; /** - * Default constructor. {@link #afterPropertiesSet()} MUST be called before the cache - * may be used when the cache is constructed using the default constructor. - */ - public DefaultSimpleCache() - { - } - - /** - * Constructor for programmatic use. - * @param maxItems - * @param cacheName + * Construct a cache using the specified capacity and name. + * + * @param maxItems The cache capacity. */ public DefaultSimpleCache(int maxItems, String cacheName) { - setMaxItems(maxItems); + if (maxItems < 1) + { + throw new IllegalArgumentException("maxItems must be a positive integer, but was " + maxItems); + } + setBeanName(cacheName); - afterPropertiesSet(); + + // The map will have a bounded size determined by the maxItems member variable. + map = new ConcurrentLinkedHashMap.Builder>() + .maximumWeightedCapacity(maxItems) + .concurrencyLevel(32) + .weigher(Weighers.singleton()) + .build(); } - - - + + /** + * Default constructor. Initialises the cache with a default capacity {@link #DEFAULT_CAPACITY} + * and no name. + */ + public DefaultSimpleCache() + { + this(DEFAULT_CAPACITY, null); + } + @Override public boolean contains(K key) { @@ -110,18 +115,17 @@ public final class DefaultSimpleCache @Override public String toString() { - return "DefaultSimpleCache[maxItems=" + maxItems + ", cacheName=" + cacheName + "]"; + return "DefaultSimpleCache[maxItems=" + map.capacity() + ", cacheName=" + cacheName + "]"; } /** - * Sets the maximum number of items that the cache will hold. The cache - * must be re-initialised if already in existence using {@link #afterPropertiesSet()}. + * Sets the maximum number of items that the cache will hold. * * @param maxItems */ - public synchronized void setMaxItems(int maxItems) + public void setMaxItems(int maxItems) { - this.maxItems = maxItems; + map.setCapacity(maxItems); } /** @@ -135,26 +139,4 @@ public final class DefaultSimpleCache { this.cacheName = cacheName; } - - - /** - * Initialise the cache. - * - * @throws Exception - */ - @Override - public synchronized void afterPropertiesSet() - { - if (maxItems < 1) - { - throw new IllegalArgumentException("maxItems property must be a positive integer."); - } - - // The map will have a bounded size determined by the maxItems member variable. - map = new ConcurrentLinkedHashMap.Builder>() - .maximumWeightedCapacity(maxItems) - .concurrencyLevel(32) - .weigher(Weighers.singleton()) - .build(); - } } diff --git a/source/java/org/alfresco/repo/cache/DefaultSimpleCacheTest.java b/source/java/org/alfresco/repo/cache/DefaultSimpleCacheTest.java index 0ad7d19462..f02b9f2485 100644 --- a/source/java/org/alfresco/repo/cache/DefaultSimpleCacheTest.java +++ b/source/java/org/alfresco/repo/cache/DefaultSimpleCacheTest.java @@ -43,9 +43,7 @@ public class DefaultSimpleCacheTest @Before public void setUp() throws Exception { - cache = new DefaultSimpleCache(); - cache.setMaxItems(100); - cache.afterPropertiesSet(); + cache = new DefaultSimpleCache(100, getClass().getName()); } @Test @@ -53,7 +51,6 @@ public class DefaultSimpleCacheTest { // We'll only keep the LAST 3 items cache.setMaxItems(3); - cache.afterPropertiesSet(); cache.put(1, "1"); cache.put(2, "2");