diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java index f6bdeb2..ef8ce08 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java @@ -52,6 +52,12 @@ class NodeCache implements Cache, GenerationCache, EvictionListener< private static final Set 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 memCache; @@ -132,6 +138,21 @@ class NodeCache implements Cache, GenerationCache, EvictionListener< }); } + 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 +187,9 @@ class NodeCache implements Cache, GenerationCache, EvictionListener< 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 +207,9 @@ class NodeCache implements Cache, GenerationCache, EvictionListener< @Override public void put(K key, V value) { memCache.put(key, value); + if (!ASYNC_CACHE) { + write((K) key, value); + } broadcast(key, value); } @@ -190,7 +217,11 @@ class NodeCache implements Cache, GenerationCache, EvictionListener< @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 +276,9 @@ class NodeCache implements Cache, GenerationCache, EvictionListener< memCache.put(key, value); } stats.markRecvBroadcast(); + if (!ASYNC_CACHE) { + write(key, value); + } } /** @@ -252,7 +286,7 @@ class NodeCache implements Cache, GenerationCache, EvictionListener< */ @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);