Merged HEAD-BUG-FIX (4.3/Cloud) to HEAD (4.3/Cloud)

59851: Merged BRANCHES/DEV/mward/head_bf_gdata_upgd to BRANCHES/DEV/HEAD-BUG-FIX (local cache improvements):
      59586: DefaultSimpleCache max size is Integer.MAX_VALUE when configured with maxItems of 0.
      59590: maxItems for DefaultSimpleCache is now an optional feature.
      59592: non-clustered caches will not use size-based eviction when {cacheName}.eviction-policy=NONE, to match clustered caches.
      59594: Added TTL to DefaultSimpleCache - not yet configurable through the factory.
      59602: DefaultCacheFactory can create caches with a time-to-live setting enabled.
      59620: Organise imports for DefaultSimpleCache
      59622: DefaultSimpleCache: changed field name from map to cache, as this makes more sense with changing to use of CacheBuilder.
      59627: Added maxIdleSecs property to DefaultSimpleCache - not yet configurable from the cache factory.
      59629: DefaultCacheFactory now supports maxIdleSeconds property for configuring non-clustered caches.
      59633: Updated description of supported non-clustered cache properties in caches.properties


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@62192 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Alan Davis
2014-02-12 08:06:59 +00:00
parent bdba166058
commit 79ccf18f85
5 changed files with 263 additions and 32 deletions

View File

@@ -21,6 +21,7 @@ package org.alfresco.repo.cache;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.BeanNameAware;
@@ -35,61 +36,94 @@ import com.google.common.cache.CacheBuilder;
public final class DefaultSimpleCache<K extends Serializable, V extends Object>
implements SimpleCache<K, V>, BeanNameAware
{
private static final int DEFAULT_CAPACITY = 200000;
private Cache<K, AbstractMap.SimpleImmutableEntry<K, V>> map;
private static final int DEFAULT_CAPACITY = Integer.MAX_VALUE;
private Cache<K, AbstractMap.SimpleImmutableEntry<K, V>> cache;
private String cacheName;
private final int maxItems;
private final boolean useMaxItems;
private final int ttlSecs;
private final int maxIdleSecs;
/**
* Construct a cache using the specified capacity and name.
*
* @param maxItems The cache capacity.
* @param maxItems The cache capacity. 0 = use {@link #DEFAULT_CAPACITY}
* @param useMaxItems Whether the maxItems value should be applied as a size-cap for the cache.
* @param cacheName An arbitrary cache name.
*/
public DefaultSimpleCache(int maxItems, String cacheName)
@SuppressWarnings("unchecked")
public DefaultSimpleCache(int maxItems, boolean useMaxItems, int ttlSecs, int maxIdleSecs, String cacheName)
{
if (maxItems < 1)
{
throw new IllegalArgumentException("maxItems must be a positive integer, but was " + maxItems);
}
else if (maxItems == 0)
if (maxItems == 0)
{
maxItems = DEFAULT_CAPACITY;
}
else if (maxItems < 0)
{
throw new IllegalArgumentException("maxItems may not be negative, but was " + maxItems);
}
this.maxItems = maxItems;
this.useMaxItems = useMaxItems;
this.ttlSecs = ttlSecs;
this.maxIdleSecs = maxIdleSecs;
setBeanName(cacheName);
// The map will have a bounded size determined by the maxItems member variable.
map = CacheBuilder.newBuilder()
.maximumSize(maxItems)
.concurrencyLevel(32)
.build();
@SuppressWarnings("rawtypes")
CacheBuilder builder = CacheBuilder.newBuilder();
if (useMaxItems)
{
builder.maximumSize(maxItems);
}
if (ttlSecs > 0)
{
builder.expireAfterWrite(ttlSecs, TimeUnit.SECONDS);
}
if (maxIdleSecs > 0)
{
builder.expireAfterAccess(maxIdleSecs, TimeUnit.SECONDS);
}
builder.concurrencyLevel(32);
cache = (Cache<K, AbstractMap.SimpleImmutableEntry<K, V>>) builder.build();
}
/**
* Default constructor. Initialises the cache with a default capacity {@link #DEFAULT_CAPACITY}
* and no name.
* Create a size limited, named cache with no other features enabled.
*
* @param maxItems
* @param cacheName
*/
public DefaultSimpleCache(int maxItems, String cacheName)
{
this(maxItems, true, 0, 0, cacheName);
}
/**
* Default constructor. Initialises the cache with no size limit and no name.
*/
public DefaultSimpleCache()
{
this(DEFAULT_CAPACITY, null);
this(0, false, 0, 0, null);
}
@Override
public boolean contains(K key)
{
return map.asMap().containsKey(key);
return cache.asMap().containsKey(key);
}
@Override
public Collection<K> getKeys()
{
return map.asMap().keySet();
return cache.asMap().keySet();
}
@Override
public V get(K key)
{
AbstractMap.SimpleImmutableEntry<K, V> kvp = map.getIfPresent(key);
AbstractMap.SimpleImmutableEntry<K, V> kvp = cache.getIfPresent(key);
if (kvp == null)
{
return null;
@@ -111,26 +145,26 @@ public final class DefaultSimpleCache<K extends Serializable, V extends Object>
public boolean putAndCheckUpdate(K key, V value)
{
AbstractMap.SimpleImmutableEntry<K, V> kvp = new AbstractMap.SimpleImmutableEntry<K, V>(key, value);
AbstractMap.SimpleImmutableEntry<K, V> priorKVP = map.asMap().put(key, kvp);
AbstractMap.SimpleImmutableEntry<K, V> priorKVP = cache.asMap().put(key, kvp);
return priorKVP != null && (! priorKVP.equals(kvp));
}
@Override
public void remove(K key)
{
map.invalidate(key);
cache.invalidate(key);
}
@Override
public void clear()
{
map.invalidateAll();
cache.invalidateAll();
}
@Override
public String toString()
{
return "DefaultSimpleCache[maxItems=" + maxItems + ", cacheName=" + cacheName + "]";
return "DefaultSimpleCache[maxItems=" + maxItems + ", useMaxItems=" + useMaxItems + ", cacheName=" + cacheName + "]";
}
/**
@@ -143,7 +177,36 @@ public final class DefaultSimpleCache<K extends Serializable, V extends Object>
return maxItems;
}
/**
* Is a size-cap in use?
*
* @return useMaxItems
*/
public boolean isUseMaxItems()
{
return this.useMaxItems;
}
/**
* Get the time-to-live setting in seconds.
*
* @return ttlSecs
*/
public int getTTLSecs()
{
return this.ttlSecs;
}
/**
* Get the time-to-idle setting in seconds.
*
* @return maxIdleSecs
*/
public int getMaxIdleSecs()
{
return this.maxIdleSecs;
}
/**
* Retrieve the name of this cache.
*