ALF-15888: change DefaultSimpleCache to initialise itself in constructor rather than using an initialising bean.

Cleaner code, no longer requires afterPropertiesSet() to be called for initialisation.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@42254 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2012-10-02 09:10:41 +00:00
parent 4550f97fc9
commit dcce548a8d
2 changed files with 33 additions and 54 deletions

View File

@@ -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,33 +33,42 @@ import com.googlecode.concurrentlinkedhashmap.Weighers;
* @author Matt Ward
*/
public final class DefaultSimpleCache<K extends Serializable, V extends Object>
implements SimpleCache<K, V>, BeanNameAware, InitializingBean
implements SimpleCache<K, V>, BeanNameAware
{
private Map<K, AbstractMap.SimpleImmutableEntry<K, V>> map;
private int maxItems = 1000000;
private static final int DEFAULT_CAPACITY = 200000;
private ConcurrentLinkedHashMap<K, AbstractMap.SimpleImmutableEntry<K, V>> 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);
setBeanName(cacheName);
afterPropertiesSet();
if (maxItems < 1)
{
throw new IllegalArgumentException("maxItems must be a positive integer, but was " + maxItems);
}
setBeanName(cacheName);
// The map will have a bounded size determined by the maxItems member variable.
map = new ConcurrentLinkedHashMap.Builder<K, AbstractMap.SimpleImmutableEntry<K, V>>()
.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<K extends Serializable, V extends Object>
@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<K extends Serializable, V extends Object>
{
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<K, AbstractMap.SimpleImmutableEntry<K, V>>()
.maximumWeightedCapacity(maxItems)
.concurrencyLevel(32)
.weigher(Weighers.singleton())
.build();
}
}

View File

@@ -43,9 +43,7 @@ public class DefaultSimpleCacheTest
@Before
public void setUp() throws Exception
{
cache = new DefaultSimpleCache<Integer, String>();
cache.setMaxItems(100);
cache.afterPropertiesSet();
cache = new DefaultSimpleCache<Integer, String>(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");