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

59850: Merged BRANCHES/DEV/mward/head_bf_gdata_upgd to BRANCHES/DEV/HEAD-BUG-FIX (upgrade gdata libs):
      59515: Removed gdata-1.45.0 libraries.
      59516: Removed concurrentlinkedhashmap-lru-1.2 library.
      59518: Added gdata-1.47.1 library JARs (dependencies to follow)
      59521: Added gdata-1.47.1 dependencies (guava-11.0.2 and JSR-305)
      59524: Updated ant build files to use gdata-1.47.1
      59528: Removed outdated gdata-1.45.0 src zip.
      59530: Added gdata-1.47.1 src zip
      59531: Update eclipse .classpath with gdata 1.47.1 libraries
      59532: Exported new gdata-1.47.1 and guava-11 libs from 3rd party project
      59559: Fix DefaultSimpleCache to use a Guava Cache, replacing ConcurrentLinkedHashMap usage.
      59568: Reverted r59516: concurrentlinkedhashmap lib is required by Surf.
      59572: Replaced use of google-collections with guava in repository's pom.xml


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

View File

@@ -24,11 +24,11 @@ import java.util.Collection;
import org.springframework.beans.factory.BeanNameAware;
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.googlecode.concurrentlinkedhashmap.Weighers;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
/**
* {@link SimpleCache} implementation backed by a {@link ConcurrentLinkedHashMap}.
* {@link SimpleCache} implementation backed by a Google {@link Cache} implementation.
*
* @author Matt Ward
*/
@@ -36,8 +36,9 @@ public final class DefaultSimpleCache<K extends Serializable, V extends Object>
implements SimpleCache<K, V>, BeanNameAware
{
private static final int DEFAULT_CAPACITY = 200000;
private ConcurrentLinkedHashMap<K, AbstractMap.SimpleImmutableEntry<K, V>> map;
private Cache<K, AbstractMap.SimpleImmutableEntry<K, V>> map;
private String cacheName;
private final int maxItems;
/**
* Construct a cache using the specified capacity and name.
@@ -50,14 +51,17 @@ public final class DefaultSimpleCache<K extends Serializable, V extends Object>
{
throw new IllegalArgumentException("maxItems must be a positive integer, but was " + maxItems);
}
else if (maxItems == 0)
{
maxItems = DEFAULT_CAPACITY;
}
this.maxItems = 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)
map = CacheBuilder.newBuilder()
.maximumSize(maxItems)
.concurrencyLevel(32)
.weigher(Weighers.singleton())
.build();
}
@@ -73,19 +77,19 @@ public final class DefaultSimpleCache<K extends Serializable, V extends Object>
@Override
public boolean contains(K key)
{
return map.containsKey(key);
return map.asMap().containsKey(key);
}
@Override
public Collection<K> getKeys()
{
return map.keySet();
return map.asMap().keySet();
}
@Override
public V get(K key)
{
AbstractMap.SimpleImmutableEntry<K, V> kvp = map.get(key);
AbstractMap.SimpleImmutableEntry<K, V> kvp = map.getIfPresent(key);
if (kvp == null)
{
return null;
@@ -107,36 +111,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.put(key, kvp);
AbstractMap.SimpleImmutableEntry<K, V> priorKVP = map.asMap().put(key, kvp);
return priorKVP != null && (! priorKVP.equals(kvp));
}
@Override
public void remove(K key)
{
map.remove(key);
map.invalidate(key);
}
@Override
public void clear()
{
map.clear();
map.invalidateAll();
}
@Override
public String toString()
{
return "DefaultSimpleCache[maxItems=" + map.capacity() + ", cacheName=" + cacheName + "]";
}
/**
* Sets the maximum number of items that the cache will hold.
*
* @param maxItems
*/
public void setMaxItems(int maxItems)
{
map.setCapacity(maxItems);
return "DefaultSimpleCache[maxItems=" + maxItems + ", cacheName=" + cacheName + "]";
}
/**
@@ -146,7 +140,7 @@ public final class DefaultSimpleCache<K extends Serializable, V extends Object>
*/
public int getMaxItems()
{
return map.capacity();
return maxItems;
}