usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [04/24] usergrid git commit: Make ttl mandatory and add more tests.
Date Wed, 21 Oct 2015 20:36:21 GMT
Make ttl mandatory and add more tests.


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

Branch: refs/heads/master
Commit: 39f49d869c1892702c41d996589a710956276659
Parents: 6f5db80
Author: Dave Johnson <snoopdave@apache.org>
Authored: Mon Sep 21 14:37:31 2015 -0400
Committer: Dave Johnson <snoopdave@apache.org>
Committed: Mon Sep 21 14:37:31 2015 -0400

----------------------------------------------------------------------
 .../usergrid/persistence/cache/ScopedCache.java |  4 +-
 .../persistence/cache/impl/ScopedCacheImpl.java |  4 +-
 .../cache/impl/ScopedCacheSerialization.java    |  2 +-
 .../impl/ScopedCacheSerializationImpl.java      | 40 +++++++--
 .../persistence/cache/ScopedCacheTest.java      | 90 ++++++++++++++++++--
 5 files changed, 124 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/39f49d86/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
index 34fdabd..29c08c8 100644
--- a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
+++ b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
@@ -22,8 +22,8 @@ package org.apache.usergrid.persistence.cache;
  */
 public interface ScopedCache<K,V> {
 
-    /** Put value into scope with ttl */
-    void put( K key, V value, long ttl );
+    /** Put value into scope with ttl (null for no ttl) */
+    void put( K key, V value, Integer ttl );
 
     /** Get value from scope */
     V get( K key );

http://git-wip-us.apache.org/repos/asf/usergrid/blob/39f49d86/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
index 4ed6150..7e3bbf7 100644
--- a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
+++ b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
@@ -36,7 +36,7 @@ public class ScopedCacheImpl<K,V> implements ScopedCache<K,V>
{
     }
 
     @Override
-    public void put(K key, V value, long ttl) {
+    public void put(K key, V value, Integer ttl) {
         serializer.writeValue( scope, key, value, ttl );
     }
 
@@ -47,6 +47,6 @@ public class ScopedCacheImpl<K,V> implements ScopedCache<K,V>
{
 
     @Override
     public void invalidate() {
-        throw new UnsupportedOperationException( "TODO" ); // TODO
+        serializer.invalidate(scope);
     }
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/39f49d86/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
index 7829222..7ef5882 100644
--- a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
+++ b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
@@ -28,7 +28,7 @@ public interface ScopedCacheSerialization<K,V> extends Migration {
 
     V readValue( CacheScope scope, K key );
 
-    void writeValue( CacheScope scope, K key, V value, long ttl );
+    void writeValue( CacheScope scope, K key, V value, Integer ttl );
 
     void invalidate( CacheScope scope );
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/39f49d86/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
index 2f116d1..899d0bf 100644
--- a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
+++ b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
@@ -46,6 +46,10 @@ import org.slf4j.LoggerFactory;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Iterator;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 
 /**
@@ -113,10 +117,8 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
         Preconditions.checkNotNull(key, "key is required");
 
         // determine bucketed row-key based application UUID
-
         String rowKeyString = scope.getApplication().getUuid().toString();
         final int bucket = BUCKET_LOCATOR.getCurrentBucket(rowKeyString);
-
         final BucketScopedRowKey<String> keyRowKey =
             BucketScopedRowKey.fromKey(scope.getApplication(), rowKeyString, bucket);
 
@@ -134,23 +136,26 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
 
             } catch (NotFoundException nfe) {
                 logger.info("Value not found");
+
             } catch (IOException ioe) {
-                logger.error("Error reading column value", ioe);
+                throw new RuntimeException("Unable to read cached value", ioe);
             }
 
         } catch (ConnectionException e) {
             throw new RuntimeException("Unable to connect to cassandra", e);
         }
+
         return null;
     }
 
 
     @Override
-    public void writeValue(CacheScope scope, K key, V value, long ttl) {
+    public void writeValue(CacheScope scope, K key, V value, Integer ttl) {
 
         Preconditions.checkNotNull( scope, "scope is required");
         Preconditions.checkNotNull( key, "key is required" );
         Preconditions.checkNotNull( value, "value is required");
+        Preconditions.checkNotNull( ttl, "ttl is required");
 
         // determine bucketed row-key based application UUID
 
@@ -173,7 +178,7 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
 
         // serialize to the entry
         final MutationBatch batch = keyspace.prepareMutationBatch();
-        batch.withRow(SCOPED_CACHE, keyRowKey).putColumn(columnName, cacheBytes);
+        batch.withRow(SCOPED_CACHE, keyRowKey).putColumn(columnName, cacheBytes, ttl);
 
         executeBatch(batch);
     }
@@ -182,6 +187,31 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
     @Override
     public void invalidate(CacheScope scope) {
 
+        Preconditions.checkNotNull(scope, "scope is required");
+
+        // determine bucketed row-key based application UUID
+        String rowKeyString = scope.getApplication().getUuid().toString();
+        final int bucket = BUCKET_LOCATOR.getCurrentBucket(rowKeyString);
+        final BucketScopedRowKey<String> keyRowKey =
+            BucketScopedRowKey.fromKey(scope.getApplication(), rowKeyString, bucket);
+
+        final MutationBatch batch = keyspace.prepareMutationBatch();
+
+        batch.withRow(SCOPED_CACHE, keyRowKey).delete();
+        executeBatch(batch);
+    }
+
+
+    private class MutationBatchExec implements Callable<Void> {
+        private final MutationBatch myBatch;
+        private MutationBatchExec(MutationBatch batch) {
+            myBatch = batch;
+        }
+        @Override
+        public Void call() throws Exception {
+            myBatch.execute();
+            return null;
+        }
     }
 
 

http://git-wip-us.apache.org/repos/asf/usergrid/blob/39f49d86/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
b/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
index 683e2e9..c37bd8b 100644
--- a/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
+++ b/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
@@ -34,6 +34,8 @@ import java.util.HashMap;
 import java.util.Map;
 
 import static junit.framework.TestCase.assertNotNull;
+import static junit.framework.TestCase.assertNull;
+import static junit.framework.TestCase.fail;
 import static org.junit.Assert.assertEquals;
 
 
@@ -46,26 +48,102 @@ public class ScopedCacheTest {
 
     @Inject protected CacheFactory<String, Map<String, Object>> cf;
 
-    protected CacheScope scope;
 
-    @Before
-    public void mockApp(){
-        this.scope = new CacheScope( new SimpleId( "application" ) );
+    @Test
+    public void testBasicOperation() {
+
+        CacheScope scope = new CacheScope( new SimpleId( "application" ) );
+        ScopedCache<String, Map<String, Object>> cache = cf.getScopedCache(scope);
+        assertNotNull("should get a cache", cache);
+
+        Map<String, Object> item = new HashMap<String, Object>() {{
+            put("field1", "value1");
+        }};
+        cache.put("item", item, 60);
+
+        Map<String, Object> retrievedItem = cache.get("item");
+        assertNotNull( "should get back item", retrievedItem );
+        assertEquals("value1", retrievedItem.get("field1"));
     }
 
 
     @Test
-    public void testBasicOperation() {
+    public void testAppsGetSeparateCaches() {
+
+        // create one app scoped cache and put a_value in it
+
+        CacheScope scope1 = new CacheScope( new SimpleId( "application" ) );
+        ScopedCache<String, Map<String, Object>> cache1 = cf.getScopedCache(scope1);
+
+        Map<String, Object> item = new HashMap<String, Object>() {{
+            put("field", "a_value");
+        }};
+        cache1.put("item", item, 60);
+
+        // create a second app scoped cache and put another_value in it
+
+        CacheScope scope2 = new CacheScope( new SimpleId( "application" ) );
+        ScopedCache<String, Map<String, Object>> cache2 = cf.getScopedCache(scope2);
+
+        item = new HashMap<String, Object>() {{
+            put("field", "another_value");
+        }};
+        cache2.put("item", item, 60);
+
+        Map<String, Object> fetched1 = cache1.get("item");
+        assertNotNull( "should get back item", fetched1 );
+        assertEquals("a_value", fetched1.get("field"));
+
+        Map<String, Object> fetched2 = cache2.get("item");
+        assertNotNull( "should get back item", fetched2 );
+        assertEquals("another_value", fetched2.get("field"));
+    }
 
+
+    @Test
+    public void testInvalidateAppCache() {
+
+        CacheScope scope = new CacheScope( new SimpleId( "application" ) );
         ScopedCache<String, Map<String, Object>> cache = cf.getScopedCache(scope);
         assertNotNull("should get a cache", cache);
 
+        // cache item forever
+
         Map<String, Object> item = new HashMap<String, Object>() {{
             put("field1", "value1");
         }};
-        cache.put("item", item, 0);
+        cache.put("item", item, 60);
+
         Map<String, Object> retrievedItem = cache.get("item");
         assertNotNull( "should get back item", retrievedItem );
         assertEquals("value1", retrievedItem.get("field1"));
+
+        cache.invalidate();
+
+        assertNull(cache.get("item"));
+    }
+
+
+    @Test
+    public void testTimeout() {
+
+        CacheScope scope = new CacheScope( new SimpleId( "application" ) );
+        ScopedCache<String, Map<String, Object>> cache = cf.getScopedCache(scope);
+        assertNotNull("should get a cache", cache);
+
+        // cache item for 1 second
+
+        Map<String, Object> item = new HashMap<String, Object>() {{
+            put("field1", "value1");
+        }};
+        cache.put("item", item, 1);
+
+        Map<String, Object> retrievedItem = cache.get("item");
+        assertNotNull( "should get back item", retrievedItem );
+        assertEquals("value1", retrievedItem.get("field1"));
+
+        try { Thread.sleep(1000); } catch (InterruptedException ignored) {}
+
+        assertNull( cache.get("item"));
     }
 }


Mime
View raw message