mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
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:
@@ -20,13 +20,9 @@ package org.alfresco.repo.cache;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.AbstractMap;
|
import java.util.AbstractMap;
|
||||||
import java.util.AbstractMap.SimpleImmutableEntry;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanNameAware;
|
import org.springframework.beans.factory.BeanNameAware;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
|
|
||||||
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
|
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
|
||||||
import com.googlecode.concurrentlinkedhashmap.Weighers;
|
import com.googlecode.concurrentlinkedhashmap.Weighers;
|
||||||
@@ -37,34 +33,43 @@ import com.googlecode.concurrentlinkedhashmap.Weighers;
|
|||||||
* @author Matt Ward
|
* @author Matt Ward
|
||||||
*/
|
*/
|
||||||
public final class DefaultSimpleCache<K extends Serializable, V extends Object>
|
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 static final int DEFAULT_CAPACITY = 200000;
|
||||||
private int maxItems = 1000000;
|
private ConcurrentLinkedHashMap<K, AbstractMap.SimpleImmutableEntry<K, V>> map;
|
||||||
private String cacheName;
|
private String cacheName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor. {@link #afterPropertiesSet()} MUST be called before the cache
|
* Construct a cache using the specified capacity and name.
|
||||||
* may be used when the cache is constructed using the default constructor.
|
*
|
||||||
*/
|
* @param maxItems The cache capacity.
|
||||||
public DefaultSimpleCache()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor for programmatic use.
|
|
||||||
* @param maxItems
|
|
||||||
* @param cacheName
|
|
||||||
*/
|
*/
|
||||||
public DefaultSimpleCache(int maxItems, String cacheName)
|
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);
|
setBeanName(cacheName);
|
||||||
afterPropertiesSet();
|
|
||||||
|
// 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
|
@Override
|
||||||
public boolean contains(K key)
|
public boolean contains(K key)
|
||||||
{
|
{
|
||||||
@@ -110,18 +115,17 @@ public final class DefaultSimpleCache<K extends Serializable, V extends Object>
|
|||||||
@Override
|
@Override
|
||||||
public String toString()
|
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
|
* Sets the maximum number of items that the cache will hold.
|
||||||
* must be re-initialised if already in existence using {@link #afterPropertiesSet()}.
|
|
||||||
*
|
*
|
||||||
* @param maxItems
|
* @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;
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -43,9 +43,7 @@ public class DefaultSimpleCacheTest
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception
|
public void setUp() throws Exception
|
||||||
{
|
{
|
||||||
cache = new DefaultSimpleCache<Integer, String>();
|
cache = new DefaultSimpleCache<Integer, String>(100, getClass().getName());
|
||||||
cache.setMaxItems(100);
|
|
||||||
cache.afterPropertiesSet();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -53,7 +51,6 @@ public class DefaultSimpleCacheTest
|
|||||||
{
|
{
|
||||||
// We'll only keep the LAST 3 items
|
// We'll only keep the LAST 3 items
|
||||||
cache.setMaxItems(3);
|
cache.setMaxItems(3);
|
||||||
cache.afterPropertiesSet();
|
|
||||||
|
|
||||||
cache.put(1, "1");
|
cache.put(1, "1");
|
||||||
cache.put(2, "2");
|
cache.put(2, "2");
|
||||||
|
Reference in New Issue
Block a user