comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java @ 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 8b442223741c
comparison
equal deleted inserted replaced
541:3cbf11c67fdc 542:f0b6d0e2a0f6
4 4
5 import net.sf.ehcache.Cache; 5 import net.sf.ehcache.Cache;
6 import net.sf.ehcache.Element; 6 import net.sf.ehcache.Element;
7 import net.sf.ehcache.CacheManager; 7 import net.sf.ehcache.CacheManager;
8 8
9 import org.apache.log4j.Logger;
9 10
10 import de.intevation.gnv.geobackend.base.Result; 11 import de.intevation.gnv.geobackend.base.Result;
11 12
12 /** 13 /**
13 * @author Sascha L. Teichmann <sascha.teichmann@intevation.de> 14 * @author Sascha L. Teichmann <sascha.teichmann@intevation.de>
17 extends QueryExecutorFactory 18 extends QueryExecutorFactory
18 { 19 {
19 public static final String QUERY_EXECUTOR_FACTORY = "caching.query.executor.config"; 20 public static final String QUERY_EXECUTOR_FACTORY = "caching.query.executor.config";
20 public static final String CACHE_NAME = "sql.cache"; 21 public static final String CACHE_NAME = "sql.cache";
21 22
23 private static Logger log = Logger.getLogger(CachingQueryExecutorFactory.class);
24
22 protected CacheManager manager; 25 protected CacheManager manager;
23 26
24 public CachingQueryExecutorFactory() { 27 public CachingQueryExecutorFactory() {
28 log.info("using SQL cache");
25 String configFile = System.getProperty(QUERY_EXECUTOR_FACTORY); 29 String configFile = System.getProperty(QUERY_EXECUTOR_FACTORY);
26 manager = configFile != null 30 manager = configFile != null
27 ? new CacheManager(configFile) 31 ? new CacheManager(configFile)
28 : new CacheManager(); 32 : new CacheManager();
29 manager.addCache(CACHE_NAME); 33 manager.addCache(CACHE_NAME);
33 return new DefaultQueryExceutor() { 37 return new DefaultQueryExceutor() {
34 38
35 public Collection<Result> cachedResults(String query) { 39 public Collection<Result> cachedResults(String query) {
36 Cache cache = manager.getCache(CACHE_NAME); 40 Cache cache = manager.getCache(CACHE_NAME);
37 Element element = cache.get(query); 41 Element element = cache.get(query);
42 if (log.isDebugEnabled()) {
43 log.debug("found results in SQL cache: " + (element != null));
44 }
38 return element != null 45 return element != null
39 ? (Collection<Result>)element.getObjectValue() 46 ? (Collection<Result>)element.getObjectValue()
40 : null; 47 : null;
41 } 48 }
42 49
43 public void cacheResult(String query, Collection<Result> results) { 50 public void cacheResults(String query, Collection<Result> results) {
51 log.debug("store results in SQL cache");
44 Cache cache = manager.getCache(CACHE_NAME); 52 Cache cache = manager.getCache(CACHE_NAME);
45 cache.put(new Element(query, results)); 53 cache.put(new Element(query, results));
46 } 54 }
47 }; 55 };
48 } 56 }
57
58 public void shutdown() {
59 log.info("shutting down SQL cache");
60 manager.getCache(CACHE_NAME).flush();
61 manager.shutdown();
62 }
49 } 63 }
50 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 64 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org