Author: thomasm
Date: Tue Mar 15 13:52:44 2016
New Revision: 1735077

URL: http://svn.apache.org/viewvc?rev=1735077&view=rev
Log:
OAK-4123 Persistent cache: allow to configure the add data concurrency

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java?rev=1735077&r1=1735076&r2=1735077&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java
 Tue Mar 15 13:52:44 2016
@@ -52,6 +52,12 @@ class NodeCache<K, V> implements Cache<K
 
     private static final Set<RemovalCause> EVICTION_CAUSES = 
ImmutableSet.of(COLLECTED, EXPIRED, SIZE);
 
+    /**
+     * Whether to use the queue to put items into cache. Default: false (cache
+     * will be updated synchronously).
+     */
+    private static final boolean ASYNC_CACHE = 
Boolean.getBoolean("oak.cache.asynchronous");
+
     private final PersistentCache cache;
     private final PersistentCacheStats stats;
     private final Cache<K, V> memCache;
@@ -76,7 +82,11 @@ class NodeCache<K, V> implements Cache<K
         map = new MultiGenerationMap<K, V>();
         keyType = new KeyDataType(type);
         valueType = new ValueDataType(docNodeStore, docStore, type);
-        this.writerQueue = new CacheWriteQueue<K, V>(dispatcher, cache, map);
+        if (ASYNC_CACHE) {
+            this.writerQueue = new CacheWriteQueue<K, V>(dispatcher, cache, 
map);
+        } else {
+            this.writerQueue = null;
+        }
         this.stats = new PersistentCacheStats(type, statisticsProvider);
     }
     
@@ -105,7 +115,7 @@ class NodeCache<K, V> implements Cache<K
     }
     
     private V readIfPresent(K key) {
-        if (writerQueue.waitsForInvalidation(key)) {
+        if (ASYNC_CACHE && writerQueue.waitsForInvalidation(key)) {
             return null;
         }
         cache.switchGenerationIfNeeded();
@@ -132,6 +142,21 @@ class NodeCache<K, V> implements Cache<K
         });
     }
 
+    private void write(final K key, final V value) {
+        cache.switchGenerationIfNeeded();
+        if (value == null) {
+            map.remove(key);
+        } else {
+            map.put(key, value);
+
+            long memory = 0L;
+            memory += (key == null ? 0L: keyType.getMemory(key));
+            memory += (value == null ? 0L: valueType.getMemory(value));
+            stats.markBytesWritten(memory);
+            stats.markPut();
+        }
+    }
+
     @SuppressWarnings("unchecked")
     @Override
     @Nullable
@@ -166,6 +191,9 @@ class NodeCache<K, V> implements Cache<K
         try {
             value = memCache.get(key, valueLoader);
             ctx.stop();
+            if (!ASYNC_CACHE) {
+                write((K) key, value);
+            }
             broadcast(key, value);
             return value;
         } catch (ExecutionException e) {
@@ -183,6 +211,9 @@ class NodeCache<K, V> implements Cache<K
     @Override
     public void put(K key, V value) {
         memCache.put(key, value);
+        if (!ASYNC_CACHE) {
+            write((K) key, value);
+        }
         broadcast(key, value);
     }
 
@@ -190,7 +221,11 @@ class NodeCache<K, V> implements Cache<K
     @Override
     public void invalidate(Object key) {
         memCache.invalidate(key);
-        writerQueue.addInvalidate(singleton((K) key));
+        if (ASYNC_CACHE) {
+            writerQueue.addInvalidate(singleton((K) key));
+        } else {
+            write((K) key, null);
+        }
         broadcast((K) key, null);
         stats.markInvalidateOne();
     }
@@ -245,6 +280,9 @@ class NodeCache<K, V> implements Cache<K
             memCache.put(key, value);
         }
         stats.markRecvBroadcast();
+        if (!ASYNC_CACHE) {
+            write(key, value);
+        }
     }
 
     /**
@@ -252,7 +290,7 @@ class NodeCache<K, V> implements Cache<K
      */
     @Override
     public void evicted(K key, V value, RemovalCause cause) {
-        if (EVICTION_CAUSES.contains(cause) && value != null) { 
+        if (ASYNC_CACHE && EVICTION_CAUSES.contains(cause) && value != null) { 
             // invalidations are handled separately
             writerQueue.addPut(key, value);
 


Reply via email to