# HG changeset patch # User Sascha L. Teichmann # Date 1261233921 0 # Node ID f0b6d0e2a0f69cbd52501e04d72292216819743e # Parent 3cbf11c67fdc0f6fd30f36573e8f944ce2a7c1a6 Small fixes for Ehcache based SQL results caching. geo-backend/trunk@463 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 3cbf11c67fdc -r f0b6d0e2a0f6 geo-backend/ChangeLog --- 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 + + * 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 - Experimental caching of SQL results via Ehache + Experimental caching of SQL results via Ehcache * pom.xml: Added dependency to Ehcache diff -r 3cbf11c67fdc -r f0b6d0e2a0f6 geo-backend/contrib/sql-cache.xml --- 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 @@ + diff -r 3cbf11c67fdc -r f0b6d0e2a0f6 geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java --- 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 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)element.getObjectValue() : null; } - public void cacheResult(String query, Collection results) { + public void cacheResults(String query, Collection 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: diff -r 3cbf11c67fdc -r f0b6d0e2a0f6 geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorFactory.java --- 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