changeset 542:f0b6d0e2a0f6

Small fixes for Ehcache based SQL results caching. geo-backend/trunk@463 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sat, 19 Dec 2009 14:45:21 +0000
parents 3cbf11c67fdc
children fac02bf1c685
files geo-backend/ChangeLog geo-backend/contrib/sql-cache.xml geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorFactory.java
diffstat 4 files changed, 40 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/geo-backend/ChangeLog	Fri Dec 18 16:13:52 2009 +0000
+++ b/geo-backend/ChangeLog	Sat Dec 19 14:45:21 2009 +0000
@@ -1,6 +1,17 @@
+2009-12-19	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* contrib/sql-cache.xml: Configured disk storage to survive restarts.
+
+	* src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorFactory.java:
+	  Added explicit shutdown hook.
+
+	* src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java:
+	  Fixed spelling of cacheResults() which prevented results to be stored in cache.
+	  Added some logging.
+
 2009-12-18	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
-	Experimental caching of SQL results via Ehache
+	Experimental caching of SQL results via Ehcache
 
 	* pom.xml: Added dependency to Ehcache
 
--- a/geo-backend/contrib/sql-cache.xml	Fri Dec 18 16:13:52 2009 +0000
+++ b/geo-backend/contrib/sql-cache.xml	Sat Dec 19 14:45:21 2009 +0000
@@ -1,12 +1,13 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <ehcache>
+    <diskStore path="java.io.tmpdir/sql-cache"/>
     <defaultCache 
         maxElementsInMemory="100"
         eternal="false"
         overflowToDisk="true"
         memoryStoreEvictionPolicy="LFU"
         diskPersistent="true"
-        maxElementsOnDisk="256"
+        maxElementsOnDisk="512"
         timeToIdleSeconds="10800"
         timeToLiveSeconds="14400"
         />
--- a/geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java	Fri Dec 18 16:13:52 2009 +0000
+++ b/geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java	Sat Dec 19 14:45:21 2009 +0000
@@ -6,6 +6,7 @@
 import net.sf.ehcache.Element;
 import net.sf.ehcache.CacheManager;
 
+import org.apache.log4j.Logger;
 
 import de.intevation.gnv.geobackend.base.Result;
 
@@ -19,9 +20,12 @@
     public static final String QUERY_EXECUTOR_FACTORY = "caching.query.executor.config";
     public static final String CACHE_NAME             = "sql.cache";
 
+    private static Logger log = Logger.getLogger(CachingQueryExecutorFactory.class);
+
     protected CacheManager manager;
 
     public CachingQueryExecutorFactory() {
+        log.info("using SQL cache");
         String configFile = System.getProperty(QUERY_EXECUTOR_FACTORY);
         manager = configFile != null
             ? new CacheManager(configFile)
@@ -35,16 +39,26 @@
             public Collection<Result> cachedResults(String query) {
                 Cache   cache   = manager.getCache(CACHE_NAME);
                 Element element = cache.get(query);
+                if (log.isDebugEnabled()) {
+                    log.debug("found results in SQL cache: " + (element != null));
+                }
                 return element != null
                     ? (Collection<Result>)element.getObjectValue()
                     : null;
             }
 
-            public void cacheResult(String query, Collection<Result> results) {
+            public void cacheResults(String query, Collection<Result> results) {
+                log.debug("store results in SQL cache");
                 Cache cache = manager.getCache(CACHE_NAME);
                 cache.put(new Element(query, results));
             }
         };
     }
+
+    public void shutdown() {
+        log.info("shutting down SQL cache");
+        manager.getCache(CACHE_NAME).flush();
+        manager.shutdown();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- a/geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorFactory.java	Fri Dec 18 16:13:52 2009 +0000
+++ b/geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorFactory.java	Sat Dec 19 14:45:21 2009 +0000
@@ -41,8 +41,14 @@
             if (className != null) {
                 try {
                     Class clazz = Class.forName(className);
-                    instance = (QueryExecutorFactory)clazz.newInstance();
-                    return instance;
+                    final QueryExecutorFactory factory =
+                        (QueryExecutorFactory)clazz.newInstance();
+                    Runtime.getRuntime().addShutdownHook(new Thread() {
+                        public void run() {
+                            factory.shutdown();
+                        }
+                    });
+                    return instance = factory;
                 }
                 catch (ClassNotFoundException cnfe) {
                     log.error(cnfe);
@@ -62,7 +68,9 @@
         }
         return instance;
     }
-    
+
+    public void shutdown() {
+    }
     
     /**
      * Getting the QueryExecutor

http://dive4elements.wald.intevation.org