usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From toddn...@apache.org
Subject [34/50] incubator-usergrid git commit: add cache fig
Date Mon, 13 Apr 2015 17:45:59 GMT
add cache fig


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

Branch: refs/heads/USERGRID-536
Commit: 803941cbd05352b54aabbe231f977fe5f193dbd5
Parents: 0a3f9db
Author: Shawn Feldman <sfeldman@apache.org>
Authored: Wed Apr 8 11:26:20 2015 -0600
Committer: Shawn Feldman <sfeldman@apache.org>
Committed: Wed Apr 8 11:26:20 2015 -0600

----------------------------------------------------------------------
 .../corepersistence/ApplicationIdCacheFig.java  | 46 ++++++++++++++++++++
 .../corepersistence/ApplicationIdCacheImpl.java | 34 +++++++++------
 .../usergrid/corepersistence/CoreModule.java    |  6 ++-
 3 files changed, 71 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/803941cb/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheFig.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheFig.java
b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheFig.java
new file mode 100644
index 0000000..7b7cec0
--- /dev/null
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheFig.java
@@ -0,0 +1,46 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  *  contributor license agreements.  The ASF licenses this file to You
+ *  * under the Apache License, Version 2.0 (the "License"); you may not
+ *  * use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *     http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.  For additional information regarding
+ *  * copyright in this work, please see the NOTICE file in the top level
+ *  * directory of this distribution.
+ *
+ */
+package org.apache.usergrid.corepersistence;
+
+/**
+ * Classy class class.
+ */
+
+import org.safehaus.guicyfig.Default;
+import org.safehaus.guicyfig.GuicyFig;
+import org.safehaus.guicyfig.Key;
+
+/**
+ * The config for the entity cache
+ */
+public interface ApplicationIdCacheFig extends GuicyFig {
+
+
+    @Key( "usergrid.applicationid_cache_size" )
+    @Default( "10000" )
+    int getCacheSize();
+
+    @Key( "usergrid.applicationid_cache_timeout_ms" )
+    @Default( "1000" )
+    int getCacheTimeout();
+
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/803941cb/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
index 8fb4f6b..2bf8ed1 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/ApplicationIdCacheImpl.java
@@ -38,7 +38,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import rx.Observable;
 
+import java.util.Optional;
 import java.util.UUID;
+import java.util.concurrent.TimeUnit;
 
 import static org.apache.usergrid.persistence.Schema.PROPERTY_APPLICATION_ID;
 
@@ -57,26 +59,30 @@ public class ApplicationIdCacheImpl implements ApplicationIdCache {
     private final EntityManager rootEm;
     private final CpEntityManagerFactory emf;
 
-    private final LoadingCache<String, UUID> appCache =
-        CacheBuilder.newBuilder().maximumSize( 10000 ).build( new CacheLoader<String,
UUID>() {
-            @Override
-            public UUID load( final String key ) throws Exception {
-                return fetchApplicationId( key );
-            }
-        } );
+    private final LoadingCache<String, Optional<UUID>> appCache;
+
 
 
-    public ApplicationIdCacheImpl(final EntityManagerFactory emf) {
+    public ApplicationIdCacheImpl(final EntityManagerFactory emf, ApplicationIdCacheFig fig)
{
         this.emf = (CpEntityManagerFactory)emf;
-        this.rootEm = emf.getEntityManager( emf.getManagementAppId());
+        this.rootEm = emf.getEntityManager(emf.getManagementAppId());
+        appCache = CacheBuilder.newBuilder()
+            .maximumSize(fig.getCacheSize())
+            .expireAfterWrite(fig.getCacheTimeout(), TimeUnit.MILLISECONDS)
+            .build(new CacheLoader<String, Optional<UUID>>() {
+                @Override
+                public Optional<UUID> load(final String key) throws Exception {
+                    return fetchApplicationId(key);
+                }
+            });
     }
 
     @Override
     public UUID getApplicationId( final String applicationName ) {
         try {
-            UUID optionalUuid = appCache.get( applicationName.toLowerCase() );
+            Optional<UUID> optionalUuid = appCache.get( applicationName.toLowerCase()
);
             logger.debug("Returning for key {} value {}", applicationName, optionalUuid );
-            return optionalUuid;
+            return optionalUuid.get();
         } catch (Exception e) {
             logger.debug("Returning for key {} value null", applicationName );
             return null;
@@ -87,7 +93,7 @@ public class ApplicationIdCacheImpl implements ApplicationIdCache {
     /**
      * Fetch our application id
      */
-    private UUID fetchApplicationId( final String applicationName ) {
+    private Optional<UUID> fetchApplicationId( final String applicationName ) {
 
         UUID value = null;
 
@@ -97,7 +103,7 @@ public class ApplicationIdCacheImpl implements ApplicationIdCache {
 
         try {
             if ( rootEm.getApplication() == null ) {
-                return null;
+                return Optional.empty();
             }
         } catch ( Exception e ) {
             logger.error("Error looking up management app", e);
@@ -116,7 +122,7 @@ public class ApplicationIdCacheImpl implements ApplicationIdCache {
             }else{
                 logger.debug("Could not load value for key {} ", applicationName );
             }
-            return value;
+            return value == null ? Optional.<UUID>empty() : Optional.of(value);
         }
         catch ( Exception e ) {
             throw new RuntimeException( "Unable to retrieve application id", e );

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/803941cb/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
index 62f2d1b..63abed9 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
@@ -18,6 +18,7 @@ package org.apache.usergrid.corepersistence;
 
 import org.apache.usergrid.corepersistence.migration.*;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
+import org.safehaus.guicyfig.GuicyFigModule;
 import org.springframework.context.ApplicationContext;
 
 import org.apache.usergrid.corepersistence.events.EntityDeletedHandler;
@@ -139,7 +140,10 @@ public class CoreModule  extends AbstractModule {
         plugins.addBinding().to( AppInfoMigrationPlugin.class );
         plugins.addBinding().to( MigrationModuleVersionPlugin.class );
 
-        bind( AllApplicationsObservable.class).to(AllApplicationsObservableImpl.class);
+        bind(AllApplicationsObservable.class).to(AllApplicationsObservableImpl.class);
+
+        install(new GuicyFigModule(ApplicationIdCacheFig.class));
+
     }
 
 


Mime
View raw message