You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2014/10/20 12:08:18 UTC

svn commit: r1633089 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheLIRS.java

Author: thomasm
Date: Mon Oct 20 10:08:17 2014
New Revision: 1633089

URL: http://svn.apache.org/r1633089
Log:
OAK-2216 LIRS cache: improved concurrency when using the cache loader

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheLIRS.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheLIRS.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheLIRS.java?rev=1633089&r1=1633088&r2=1633089&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheLIRS.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/cache/CacheLIRS.java Mon Oct 20 10:08:17 2014
@@ -833,9 +833,17 @@ public class CacheLIRS<K, V> implements 
             }
         }
         
-        synchronized V get(K key, int hash, Callable<? extends V> valueLoader) throws ExecutionException {
+        V get(K key, int hash, Callable<? extends V> valueLoader) throws ExecutionException {
+            // avoid synchronization if it's in the cache
             V value = get(key, hash);
-            if (value == null) {
+            if (value != null) {
+                return value;
+            }
+            synchronized (this) {
+                value = get(key, hash);
+                if (value != null) {
+                    return value;
+                }
                 long start = System.nanoTime();
                 try {
                     value = valueLoader.call();
@@ -848,15 +856,23 @@ public class CacheLIRS<K, V> implements 
                     totalLoadTime += time;
                 }
                 put(key, hash, value, cache.sizeOf(key, value));
+                return value;
             }
-            return value;
         }
         
-        synchronized V get(K key, int hash, CacheLoader<K, V> loader) throws ExecutionException {
+        V get(K key, int hash, CacheLoader<K, V> loader) throws ExecutionException {
+            // avoid synchronization if it's in the cache
             V value = get(key, hash);
-            if (value == null) {
-                if (loader == null) {
-                    return null;
+            if (value != null) {
+                return value;
+            }
+            if (loader == null) {
+                return null;
+            }
+            synchronized (this) {
+                value = get(key, hash);
+                if (value != null) {
+                    return value;
                 }
                 long start = System.nanoTime();
                 try {
@@ -870,8 +886,8 @@ public class CacheLIRS<K, V> implements 
                     totalLoadTime += time;
                 }
                 put(key, hash, value, cache.sizeOf(key, value));
+                return value;
             }
-            return value;
         }
         
         synchronized V replace(K key, int hash, V value, int memory) {