You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2012/11/22 12:09:44 UTC

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

Author: aadamchik
Date: Thu Nov 22 11:09:43 2012
New Revision: 1412502

URL: http://svn.apache.org/viewvc?rev=1412502&view=rev
Log:
CAY-1774 EhCacheQueryCache.get(QueryMetadata, QueryCacheEntryFactory) returns null if EhCache instance for group is not present

cleanup and refactoring

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=1412502&r1=1412501&r2=1412502&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 Nov 22 11:09:43 2012
@@ -71,59 +71,34 @@ public class EhCacheQueryCache implement
         if (key == null) {
             return null;
         }
-        
-        Element result = null;
-        String[] groupNames = metadata.getCacheGroups();
-        if (groupNames != null && groupNames.length > 0) {
-            Ehcache cache = cacheManager.getCache(groupNames[0]);
-            if (cache == null) {
-                return null;
-            }
-            else {
-                result = cache.get(key);
-                if (result != null) {
-                    return (List)result.getObjectValue();
-                }
-            }
-            if (groupNames.length > 1) {
-                logger.warn("multiple cache groups per key: " + key);
-            }
-        }
-        else {
-            result = getDefaultCache().get(key);
-            if (result != null) {
-                return (List)getDefaultCache().get(key).getObjectValue();
-            }
+
+        String cacheName = cacheName(key, metadata.getCacheGroups());
+        Ehcache cache = cacheManager.getCache(cacheName);
+
+        if (cache == null) {
+            return null;
         }
-        return null;
+
+        Element result = cache.get(key);
+        return result != null ? (List) result.getObjectValue() : null;
     }
 
     public List get(QueryMetadata metadata, QueryCacheEntryFactory factory) {
+
         String key = metadata.getCacheKey();
         if (key == null) {
+            // TODO: we should really throw here... The method declares that it
+            // never returns null
             return null;
         }
 
-        Ehcache cache = null;
-        Element result = null;
-        String[] groupNames = metadata.getCacheGroups();
-        if (groupNames != null && groupNames.length > 0) {
-
-            if (groupNames.length > 1) {
-                logger.warn("multiple cache groups per key '" + key + "', ignoring all but the first one: "
-                        + groupNames[0]);
-            }
-
-            // create empty cache for cache group here, as we have a factory to
-            // create an object, and should never ever return null from this
-            // method
-            cache = cacheManager.addCacheIfAbsent(groupNames[0]);
-            result = cache.get(key);
+        String cacheName = cacheName(key, metadata.getCacheGroups());
 
-        } else {
-            cache = getDefaultCache();
-            result = cache.get(key);
-        }
+        // create empty cache for cache group here, as we have a factory to
+        // create an object, and should never ever return null from this
+        // method
+        Ehcache cache = cacheManager.addCacheIfAbsent(cacheName);
+        Element result = cache.get(key);
 
         if (result != null) {
             return (List) result.getObjectValue();
@@ -134,49 +109,55 @@ public class EhCacheQueryCache implement
         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());
-                    }
-                }
+            // now that we locked the key, let's reread the cache again, in case
+            // an object appeared there already
+            result = cache.get(key);
 
-                list = (List) noResult;
-                put(metadata, list);
-                return list;
-            } else {
-                return list;
+            if (result != null) {
+                return (List) result.getObjectValue();
+            }
+
+            // if not succeeded in reading again putting
+            // object to the cache ourselves
+            Object object = factory.createObject();
+            if (!(object instanceof List)) {
+                if (object == null) {
+                    throw new CayenneRuntimeException("Null object created: " + metadata.getCacheKey());
+                } else {
+                    throw new CayenneRuntimeException("Invalid query result, expected List, got "
+                            + object.getClass().getName());
+                }
             }
+
+            cache.put(new Element(key, object));
+
+            return (List) object;
+
         } finally {
             cache.releaseWriteLockOnKey(key);
         }
     }
+    
+    private String cacheName(String key, String... cacheGroups) {
+        if (cacheGroups != null && cacheGroups.length > 0) {
+
+            if (cacheGroups.length > 1) {
+                logger.warn("multiple cache groups per key '" + key + "', ignoring all but the first one: "
+                        + cacheGroups[0]);
+            }
+
+            return cacheGroups[0];
+        }
+
+        return DEFAULT_CACHE_NAME;
+    }
 
     public void put(QueryMetadata metadata, List results) {
         String key = metadata.getCacheKey();
         if (key != null) {
-            String[] groupNames = metadata.getCacheGroups();
-            if (groupNames != null && groupNames.length > 0) {
-                Ehcache cache = cacheManager.addCacheIfAbsent(groupNames[0]);
-                cache.put(new Element(key, results));
-                if (groupNames.length > 1) {
-                    logger.warn("multiple groups per key: " + key);
-                }
-            }
-            else {
-                getDefaultCache().put(new Element(key,results));
-            }
+            String cacheName = cacheName(key, metadata.getCacheGroups());
+            Ehcache cache = cacheManager.addCacheIfAbsent(cacheName);
+            cache.put(new Element(key,results));
         }
     }
 
@@ -185,8 +166,7 @@ public class EhCacheQueryCache implement
             for (String cache : cacheManager.getCacheNames()) {
                 cacheManager.getCache(cache).remove(key);
             }
-        }
-        
+        }   
     }
 
     public void removeGroup(String groupKey) {