From bdba166058ff06fecd4be706de132459b3243e68 Mon Sep 17 00:00:00 2001 From: Alan Davis Date: Wed, 12 Feb 2014 08:06:09 +0000 Subject: [PATCH] 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 --- pom.xml | 6 +-- .../repo/cache/DefaultCacheFactory.java | 8 +--- .../repo/cache/DefaultCacheProvider.java | 3 +- .../repo/cache/DefaultSimpleCache.java | 46 ++++++++----------- .../repo/cache/DefaultSimpleCacheTest.java | 2 +- 5 files changed, 26 insertions(+), 39 deletions(-) diff --git a/pom.xml b/pom.xml index aa13db3015..3d254101d3 100644 --- a/pom.xml +++ b/pom.xml @@ -688,9 +688,9 @@ 2.0 - com.google.collections - google-collections - 1.0 + com.google.guava + guava + 11.0.2 org.springframework.security diff --git a/source/java/org/alfresco/repo/cache/DefaultCacheFactory.java b/source/java/org/alfresco/repo/cache/DefaultCacheFactory.java index 7828002c24..8f4432053f 100644 --- a/source/java/org/alfresco/repo/cache/DefaultCacheFactory.java +++ b/source/java/org/alfresco/repo/cache/DefaultCacheFactory.java @@ -45,14 +45,8 @@ public class DefaultCacheFactory extends AbstractCach private SimpleCache createLocalCache(String cacheName) { - DefaultSimpleCache cache = new DefaultSimpleCache(); - cache.setCacheName(cacheName); int maxItems = maxItems(cacheName); - // maxItems of zero has no effect, DefaultSimpleCache will use its default capacity. - if (maxItems > 0) - { - cache.setMaxItems(maxItems); - } + DefaultSimpleCache cache = new DefaultSimpleCache(maxItems, cacheName); if (log.isDebugEnabled()) { log.debug("Creating cache: " + cache); diff --git a/source/java/org/alfresco/repo/cache/DefaultCacheProvider.java b/source/java/org/alfresco/repo/cache/DefaultCacheProvider.java index dd1d9ee9ca..f5da70085e 100644 --- a/source/java/org/alfresco/repo/cache/DefaultCacheProvider.java +++ b/source/java/org/alfresco/repo/cache/DefaultCacheProvider.java @@ -47,8 +47,7 @@ public class DefaultCacheProvider implements CacheProvider { log.debug("building cache for regionName=" + regionName + ", with properties: " + properties); } - DefaultSimpleCache cache = new DefaultSimpleCache(); - cache.setMaxItems(defaultMaxItems); + DefaultSimpleCache cache = new DefaultSimpleCache(defaultMaxItems, null); Cache hibCache = new HibernateSimpleCacheAdapter(cache, regionName); return hibCache; } diff --git a/source/java/org/alfresco/repo/cache/DefaultSimpleCache.java b/source/java/org/alfresco/repo/cache/DefaultSimpleCache.java index 6d75d23301..93648b1fab 100644 --- a/source/java/org/alfresco/repo/cache/DefaultSimpleCache.java +++ b/source/java/org/alfresco/repo/cache/DefaultSimpleCache.java @@ -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 implements SimpleCache, BeanNameAware { private static final int DEFAULT_CAPACITY = 200000; - private ConcurrentLinkedHashMap> map; + private Cache> 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 { 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>() - .maximumWeightedCapacity(maxItems) + map = CacheBuilder.newBuilder() + .maximumSize(maxItems) .concurrencyLevel(32) - .weigher(Weighers.singleton()) .build(); } @@ -73,19 +77,19 @@ public final class DefaultSimpleCache @Override public boolean contains(K key) { - return map.containsKey(key); + return map.asMap().containsKey(key); } @Override public Collection getKeys() { - return map.keySet(); + return map.asMap().keySet(); } @Override public V get(K key) { - AbstractMap.SimpleImmutableEntry kvp = map.get(key); + AbstractMap.SimpleImmutableEntry kvp = map.getIfPresent(key); if (kvp == null) { return null; @@ -107,36 +111,26 @@ public final class DefaultSimpleCache public boolean putAndCheckUpdate(K key, V value) { AbstractMap.SimpleImmutableEntry kvp = new AbstractMap.SimpleImmutableEntry(key, value); - AbstractMap.SimpleImmutableEntry priorKVP = map.put(key, kvp); + AbstractMap.SimpleImmutableEntry 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 */ public int getMaxItems() { - return map.capacity(); + return maxItems; } diff --git a/source/test-java/org/alfresco/repo/cache/DefaultSimpleCacheTest.java b/source/test-java/org/alfresco/repo/cache/DefaultSimpleCacheTest.java index 1299eda236..f3e5b1b511 100644 --- a/source/test-java/org/alfresco/repo/cache/DefaultSimpleCacheTest.java +++ b/source/test-java/org/alfresco/repo/cache/DefaultSimpleCacheTest.java @@ -41,7 +41,7 @@ public class DefaultSimpleCacheTest extends SimpleCacheTestBase(3, getClass().getName()); cache.put(1, "1"); cache.put(2, "2");