Mercurial > dive4elements > gnv-client
comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java @ 541:3cbf11c67fdc
Experimental caching of SQL results via Ehache
geo-backend/trunk@462 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 18 Dec 2009 16:13:52 +0000 |
parents | |
children | f0b6d0e2a0f6 |
comparison
equal
deleted
inserted
replaced
385:27082b83c267 | 541:3cbf11c67fdc |
---|---|
1 package de.intevation.gnv.geobackend.base.query; | |
2 | |
3 import java.util.Collection; | |
4 | |
5 import net.sf.ehcache.Cache; | |
6 import net.sf.ehcache.Element; | |
7 import net.sf.ehcache.CacheManager; | |
8 | |
9 | |
10 import de.intevation.gnv.geobackend.base.Result; | |
11 | |
12 /** | |
13 * @author Sascha L. Teichmann <sascha.teichmann@intevation.de> | |
14 * | |
15 */ | |
16 public class CachingQueryExecutorFactory | |
17 extends QueryExecutorFactory | |
18 { | |
19 public static final String QUERY_EXECUTOR_FACTORY = "caching.query.executor.config"; | |
20 public static final String CACHE_NAME = "sql.cache"; | |
21 | |
22 protected CacheManager manager; | |
23 | |
24 public CachingQueryExecutorFactory() { | |
25 String configFile = System.getProperty(QUERY_EXECUTOR_FACTORY); | |
26 manager = configFile != null | |
27 ? new CacheManager(configFile) | |
28 : new CacheManager(); | |
29 manager.addCache(CACHE_NAME); | |
30 } | |
31 | |
32 public QueryExecutor getQueryExecutor() { | |
33 return new DefaultQueryExceutor() { | |
34 | |
35 public Collection<Result> cachedResults(String query) { | |
36 Cache cache = manager.getCache(CACHE_NAME); | |
37 Element element = cache.get(query); | |
38 return element != null | |
39 ? (Collection<Result>)element.getObjectValue() | |
40 : null; | |
41 } | |
42 | |
43 public void cacheResult(String query, Collection<Result> results) { | |
44 Cache cache = manager.getCache(CACHE_NAME); | |
45 cache.put(new Element(query, results)); | |
46 } | |
47 }; | |
48 } | |
49 } | |
50 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |