You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by to...@apache.org on 2014/10/30 04:39:50 UTC

[24/45] git commit: Use Guava Cache for manager caches.

Use Guava Cache for manager caches.


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/3d3f1897
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/3d3f1897
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/3d3f1897

Branch: refs/heads/key-row-sharding
Commit: 3d3f1897953bffa96b18a9012b34c0480c849cee
Parents: 8df2185
Author: Dave Johnson <dm...@apigee.com>
Authored: Wed Oct 29 09:07:05 2014 -0400
Committer: Dave Johnson <dm...@apigee.com>
Committed: Wed Oct 29 09:07:05 2014 -0400

----------------------------------------------------------------------
 .../corepersistence/CpManagerCache.java         | 117 +++++++++++--------
 1 file changed, 70 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/3d3f1897/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java
index 62ea81f..99bde22 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java
@@ -15,6 +15,11 @@
  */
 package org.apache.usergrid.corepersistence;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 import org.apache.usergrid.persistence.collection.CollectionScope;
 import org.apache.usergrid.persistence.collection.EntityCollectionManager;
 import org.apache.usergrid.persistence.collection.EntityCollectionManagerFactory;
@@ -26,7 +31,7 @@ import org.apache.usergrid.persistence.index.EntityIndexFactory;
 import org.apache.usergrid.persistence.map.MapManager;
 import org.apache.usergrid.persistence.map.MapManagerFactory;
 import org.apache.usergrid.persistence.map.MapScope;
-import org.apache.usergrid.utils.LRUCache2;
+
 
 public class CpManagerCache {
 
@@ -36,19 +41,50 @@ public class CpManagerCache {
     private final MapManagerFactory mmf;
 
     // TODO: consider making these cache sizes and timeouts configurable
-    // TODO: replace with Guava cache
-    private final LRUCache2<CollectionScope, EntityCollectionManager> ecmCache
-            = new LRUCache2<CollectionScope, EntityCollectionManager>(50, 1 * 60 * 60 * 1000);
-
-    private final LRUCache2<ApplicationScope, EntityIndex> eiCache
-            = new LRUCache2<>(50, 1 * 60 * 60 * 1000);
-
-    private final LRUCache2<ApplicationScope, GraphManager> gmCache
-            = new LRUCache2<ApplicationScope, GraphManager>(50, 1 * 60 * 60 * 1000);
-
-    private final LRUCache2<MapScope, MapManager> mmCache
-            = new LRUCache2<MapScope, MapManager>(50, 1 * 60 * 60 * 1000);
 
+    private LoadingCache<CollectionScope, EntityCollectionManager> ecmCache = 
+        CacheBuilder.newBuilder()
+            .maximumSize(100)
+            .expireAfterWrite( 1, TimeUnit.HOURS)
+            .build( new CacheLoader<CollectionScope, EntityCollectionManager>() {
+                public EntityCollectionManager load( CollectionScope scope ) { 
+                    return ecmf.createCollectionManager( scope );
+                }
+            }
+        );
+
+    private LoadingCache<ApplicationScope, EntityIndex> eiCache = 
+        CacheBuilder.newBuilder()
+            .maximumSize(100)
+            .expireAfterWrite( 1, TimeUnit.HOURS )
+            .build( new CacheLoader<ApplicationScope, EntityIndex>() {
+                public EntityIndex load( ApplicationScope scope ) { 
+                    return eif.createEntityIndex( scope );
+                }
+            }
+        );
+
+    private LoadingCache<ApplicationScope, GraphManager> gmCache = 
+        CacheBuilder.newBuilder()
+            .maximumSize(100)
+            .expireAfterWrite( 1, TimeUnit.HOURS )
+            .build( new CacheLoader<ApplicationScope, GraphManager>() {
+                public GraphManager load( ApplicationScope scope ) { 
+                    return gmf.createEdgeManager( scope );
+                }
+            }
+        );
+
+    private LoadingCache<MapScope, MapManager> mmCache = 
+        CacheBuilder.newBuilder()
+            .maximumSize(100)
+            .expireAfterWrite( 1, TimeUnit.HOURS )
+            .build( new CacheLoader<MapScope, MapManager>() {
+                public MapManager load( MapScope scope ) { 
+                    return mmf.createMapManager( scope );
+                }
+            }
+        );
 
     public CpManagerCache(
             EntityCollectionManagerFactory ecmf, 
@@ -63,54 +99,41 @@ public class CpManagerCache {
     }
 
     public EntityCollectionManager getEntityCollectionManager(CollectionScope scope) {
-
-        EntityCollectionManager ecm = ecmCache.get(scope);
-
-        if (ecm == null) {
-            ecm = ecmf.createCollectionManager(scope);
-            ecmCache.put(scope, ecm);
+        try {
+            return ecmCache.get( scope );
+        } catch (ExecutionException ex) {
+            throw new RuntimeException("Error getting manager", ex);
         }
-        return ecm;
     }
 
-    public EntityIndex getEntityIndex(ApplicationScope applicationScope) {
-
-        EntityIndex ei = eiCache.get(applicationScope);
-
-        if (ei == null) {
-            ei = eif.createEntityIndex(applicationScope);
-            eiCache.put(applicationScope, ei);
+    public EntityIndex getEntityIndex(ApplicationScope appScope) {
+        try {
+            return eiCache.get( appScope );
+        } catch (ExecutionException ex) {
+            throw new RuntimeException("Error getting manager", ex);
         }
-        return ei;
     }
 
     public GraphManager getGraphManager(ApplicationScope appScope) {
-
-        GraphManager gm = gmCache.get(appScope);
-
-        if (gm == null) {
-            gm = gmf.createEdgeManager(appScope);
-            gmCache.put(appScope, gm);
+        try {
+            return gmCache.get( appScope );
+        } catch (ExecutionException ex) {
+            throw new RuntimeException("Error getting manager", ex);
         }
-        return gm;
     }
 
     public MapManager getMapManager( MapScope mapScope) {
-
-        MapManager mm = mmCache.get(mapScope);
-
-        if (mm == null) {
-            mm = mmf.createMapManager(mapScope);
-            mmCache.put(mapScope, mm);
+        try {
+            return mmCache.get( mapScope );
+        } catch (ExecutionException ex) {
+            throw new RuntimeException("Error getting manager", ex);
         }
-        return mm;
     }
 
     void flush() {
-        gmCache.purge();
-        ecmCache.purge();
-        eiCache.purge();
+        ecmCache.invalidateAll();
+        eiCache.invalidateAll();
+        gmCache.invalidateAll();
+        mmCache.invalidateAll();
     }
-
-
 }