You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2015/10/21 22:36:33 UTC

[16/24] usergrid git commit: Setting cache TTL to zero will disable the cache, also: simplified logic that finds application ID in the key.

Setting cache TTL to zero will disable the cache, also: simplified logic that finds application ID in the key.


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

Branch: refs/heads/master
Commit: 513eae4ac008fb094ecd19dd2b59fbb162520174
Parents: 030335d
Author: Dave Johnson <sn...@apache.org>
Authored: Wed Sep 30 09:56:05 2015 -0400
Committer: Dave Johnson <sn...@apache.org>
Committed: Wed Sep 30 09:56:05 2015 -0400

----------------------------------------------------------------------
 .../main/resources/usergrid-default.properties  |  1 +
 .../rest/security/shiro/ShiroCache.java         | 65 ++++++++------------
 .../rest/security/shiro/ShiroCacheManager.java  | 10 +--
 3 files changed, 29 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/513eae4a/stack/config/src/main/resources/usergrid-default.properties
----------------------------------------------------------------------
diff --git a/stack/config/src/main/resources/usergrid-default.properties b/stack/config/src/main/resources/usergrid-default.properties
index bd79725..7b80a23 100644
--- a/stack/config/src/main/resources/usergrid-default.properties
+++ b/stack/config/src/main/resources/usergrid-default.properties
@@ -590,6 +590,7 @@ usergrid.version.build=${version}
 
 # Usergrid will cache computed authentication and authorization information in Cassandra.
 # The cache time-to-live is specified in seconds and defaults to the value below.
+# Setting to zero will disable the authentication cache.
 #
 usergrid.auth.cache.time-to-live=3600
 

http://git-wip-us.apache.org/repos/asf/usergrid/blob/513eae4a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java b/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java
index 5c063ed..7fb7422 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java
@@ -27,10 +27,7 @@ import org.apache.usergrid.persistence.cache.ScopedCache;
 import org.apache.usergrid.persistence.model.entity.SimpleId;
 import org.apache.usergrid.security.shiro.UsergridAuthenticationInfo;
 import org.apache.usergrid.security.shiro.UsergridAuthorizationInfo;
-import org.apache.usergrid.security.shiro.principals.ApplicationGuestPrincipal;
-import org.apache.usergrid.security.shiro.principals.ApplicationPrincipal;
-import org.apache.usergrid.security.shiro.principals.OrganizationPrincipal;
-import org.apache.usergrid.security.shiro.principals.UserPrincipal;
+import org.apache.usergrid.security.shiro.principals.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -59,6 +56,8 @@ public class ShiroCache<K, V> implements Cache<K,V> {
 
     @Override
     public V get(K key) throws CacheException {
+        if ( cacheTtl == 0 ) return null;
+
         ScopedCache<String, V> scopedCache = getCacheScope(key);
         if ( scopedCache != null ) {
             V value = scopedCache.get(getKeyString(key), typeRef);
@@ -84,6 +83,8 @@ public class ShiroCache<K, V> implements Cache<K,V> {
 
     @Override
     public V put(K key, V value) throws CacheException {
+        if ( cacheTtl == 0 ) return null;
+
         ScopedCache<String, V> scopedCache = getCacheScope(key);
         if ( scopedCache != null ) {
             V ret = scopedCache.put(getKeyString(key), value, cacheTtl);
@@ -106,6 +107,8 @@ public class ShiroCache<K, V> implements Cache<K,V> {
 
     @Override
     public V remove(K key) throws CacheException {
+        if ( cacheTtl == 0 ) return null;
+
         ScopedCache<String, V> scopedCache = getCacheScope(key);
         if ( scopedCache != null ) {
             scopedCache.remove( getKeyString(key) );
@@ -115,11 +118,12 @@ public class ShiroCache<K, V> implements Cache<K,V> {
 
     @Override
     public void clear() throws CacheException {
+        // no-op: Usergrid logic will invalidate cache as necessary
     }
 
     @Override
     public int size() {
-        return 0;
+        return 0; // TODO?
     }
 
     @Override
@@ -136,50 +140,38 @@ public class ShiroCache<K, V> implements Cache<K,V> {
     /** get cache for application scope */
     private ScopedCache<String, V> getCacheScope( K key ) {
 
-        UUID applicationId;
+        // get the principal
 
+        PrincipalIdentifier principal;
         if ( key instanceof SimplePrincipalCollection) {
-            SimplePrincipalCollection spc = (SimplePrincipalCollection)key;
-
-            if ( spc.getPrimaryPrincipal() instanceof UserPrincipal ) {
-                UserPrincipal p = (UserPrincipal) spc.getPrimaryPrincipal();
-                applicationId = p.getApplicationId();
+            SimplePrincipalCollection spc = (SimplePrincipalCollection) key;
+            principal = (PrincipalIdentifier) spc.getPrimaryPrincipal();
 
-            } else  if ( spc.getPrimaryPrincipal() instanceof ApplicationPrincipal ) {
-                ApplicationPrincipal p = (ApplicationPrincipal)spc.getPrimaryPrincipal();
-                applicationId = p.getApplicationId();
-
-            } else  if ( spc.getPrimaryPrincipal() instanceof OrganizationPrincipal ) {
-                applicationId = CpNamingUtils.MANAGEMENT_APPLICATION_ID;
+        } else {
+            principal = (PrincipalIdentifier)key;
+        }
 
-            } else if ( spc.getPrimaryPrincipal() instanceof ApplicationGuestPrincipal) {
-                ApplicationGuestPrincipal p = (ApplicationGuestPrincipal)spc.getPrimaryPrincipal();
-                applicationId = p.getApplicationId();
+        // get the id for the scope
 
-            } else {
-                logger.error("Unknown principal type: " + spc.getPrimaryPrincipal().getClass().getSimpleName());
-                throw new RuntimeException("Unknown principal type: "
-                    + spc.getPrimaryPrincipal().getClass().getSimpleName());
-            }
-
-        } else if ( key instanceof UserPrincipal ) {
-            UserPrincipal p = (UserPrincipal)key;
+        UUID applicationId;
+        if ( principal instanceof UserPrincipal ) {
+            UserPrincipal p = (UserPrincipal)principal;
             applicationId = p.getApplicationId();
 
-        } else if ( key instanceof ApplicationPrincipal ) {
-            ApplicationPrincipal p = (ApplicationPrincipal)key;
+        } else if ( principal instanceof ApplicationPrincipal ) {
+            ApplicationPrincipal p = (ApplicationPrincipal)principal;
             applicationId = p.getApplicationId();
 
-        } else if ( key instanceof OrganizationPrincipal ) {
+        } else if ( principal instanceof OrganizationPrincipal ) {
             applicationId = CpNamingUtils.MANAGEMENT_APPLICATION_ID;
 
-        } else if ( key instanceof ApplicationGuestPrincipal) {
-            ApplicationGuestPrincipal p = (ApplicationGuestPrincipal)key;
+        } else if ( principal instanceof ApplicationGuestPrincipal) {
+            ApplicationGuestPrincipal p = (ApplicationGuestPrincipal)principal;
             applicationId = p.getApplicationId();
 
         } else {
             logger.error("Unknown key type: " + key.getClass().getSimpleName());
-            throw new RuntimeException("Unknown key type: " + key.getClass().getSimpleName());
+            throw new RuntimeException("Unknown key type: " + principal.getClass().getSimpleName());
         }
 
         CacheScope scope = new CacheScope(new SimpleId(applicationId, "application"));
@@ -203,9 +195,4 @@ public class ShiroCache<K, V> implements Cache<K,V> {
         return key.toString() + "_" + key.getClass().getSimpleName();
     }
 
-    public void invalidate( UUID applicationId ) {
-        CacheScope scope = new CacheScope( new SimpleId(applicationId, "application") );
-        ScopedCache cache = cacheFactory.getScopedCache(scope);
-        cache.invalidate();
-    }
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/513eae4a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java b/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java
index 717ee2a..3f768ed 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java
@@ -85,7 +85,8 @@ public class ShiroCacheManager implements CacheManager {
                     getCacheTtl());
 
             } else {
-                throw new RuntimeException("Unknown Shiro cache name");
+                logger.error("Unknown Shiro Cache name: " + name);
+                throw new RuntimeException("Unknown Shiro Cache name: " + name);
             }
 
             caches.put(name, shiroCache);
@@ -93,13 +94,6 @@ public class ShiroCacheManager implements CacheManager {
         return shiroCache;
     }
 
-    public void invalidateApplicationCaches(UUID applicationId) {
-        for (String key : caches.keySet()) {
-            ShiroCache shiroCache = caches.get(key);
-            shiroCache.invalidate(applicationId);
-        }
-    }
-
     private Integer getCacheTtl() {
         if ( cacheTtl == null ) {
             String cacheTtlString = properties.getProperty(CACHE_TTL_PROPERTY_NAME);