cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dkazimirc...@apache.org
Subject svn commit: r1151922 - /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
Date Thu, 28 Jul 2011 16:43:33 GMT
Author: dkazimirchyk
Date: Thu Jul 28 16:43:32 2011
New Revision: 1151922

URL: http://svn.apache.org/viewvc?rev=1151922&view=rev
Log:
CAY-1595 EHCache implementation of Provider<QueryCache>

correcting some multithreading problems

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java?rev=1151922&r1=1151921&r2=1151922&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
Thu Jul 28 16:43:32 2011
@@ -104,27 +104,66 @@ public class EhCacheQueryCache implement
             return null;
         }
         
-        List result = get(metadata);
-        if (result != null) {
-            return result;
+        Ehcache cache = null;
+        Element result = null;
+        String[] groupNames = metadata.getCacheGroups();
+        if (groupNames != null && groupNames.length > 0) {
+            cache = cacheManager.getCache(groupNames[0]);
+            if (cache == null) {
+                return null;
+            }
+            else {
+                result = cache.get(key);
+            }
+            if (groupNames.length > 1) {
+                logger.warn("multiple cache groups per key: " + key);
+            }
         }
         else {
-            Object noResult = factory.createObject();
-            if (!(noResult instanceof List)) {
-                if (noResult == null) {
-                    throw new CayenneRuntimeException("Null object created: "
-                            + metadata.getCacheKey());
-                }
-                else {
-                    throw new CayenneRuntimeException(
-                            "Invalid query result, expected List, got "
-                            + noResult.getClass().getName());
+            cache = getDefaultCache();
+            result = cache.get(key);
+        }
+
+        if (result != null) {
+            return (List)result.getObjectValue();
+        }
+
+        // if no result in cache locking the key to write
+        // and putting it to the cache
+        cache.acquireWriteLockOnKey(key);
+        try {
+            
+            // trying to read from cache again in case of
+            // someone else put it to the cache before us
+            List list = get(metadata);
+            
+            if (list == null) {
+                
+                // if not succeeded  in reading again putting
+                // object to the cache ourselves
+                Object noResult = factory.createObject();
+                if (!(noResult instanceof List)) {
+                    if (noResult == null) {
+                        throw new CayenneRuntimeException("Null object created: "
+                                + metadata.getCacheKey());
+                    }
+                    else {
+                        throw new CayenneRuntimeException(
+                                "Invalid query result, expected List, got "
+                                + noResult.getClass().getName());
+                    }
                 }
-            }
 
-            List list = (List)noResult;
-            put(metadata, list);
-            return list;
+                list = (List)noResult;
+                put(metadata, list);
+                return list;
+            }
+            else {
+                return list;
+            }
+        }
+        finally {
+            cache.releaseWriteLockOnKey(key);
         }
     }
 



Mime
View raw message