Mercurial > dive4elements > gnv-client
annotate geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java @ 1122:1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
geo-backend/trunk@1149 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 03 Jun 2010 13:31:07 +0000 |
parents | 02cd2935b5fa |
children | f3fdbeef1c68 |
rev | line source |
---|---|
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.gnv.geobackend.base.query; |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import java.util.Collection; |
895
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
4 import java.util.Iterator; |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 import net.sf.ehcache.Cache; |
886
8b442223741c
Ordered imports. Removed empty headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
542
diff
changeset
|
7 import net.sf.ehcache.CacheManager; |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import net.sf.ehcache.Element; |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
542
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
10 import org.apache.log4j.Logger; |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 |
895
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
12 import de.intevation.gnv.geobackend.base.Result; |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
13 import de.intevation.gnv.geobackend.base.query.cache.CacheCleaner; |
1122
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
14 import de.intevation.gnv.geobackend.config.Configuration; |
895
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
15 |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 /** |
887
b757def3ff55
Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
886
diff
changeset
|
17 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 */ |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 public class CachingQueryExecutorFactory |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 extends QueryExecutorFactory |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 { |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 public static final String CACHE_NAME = "sql.cache"; |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 |
542
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
24 private static Logger log = Logger.getLogger(CachingQueryExecutorFactory.class); |
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
25 |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 protected CacheManager manager; |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 public CachingQueryExecutorFactory() { |
542
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
29 log.info("using SQL cache"); |
1122
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
30 |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
31 init(); |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
32 } |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
33 |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
34 private void init() { |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
35 Configuration config = Configuration.getInstance(); |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
36 |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
37 if (config == null) { |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
38 log.error("No geobackend configuration found. " + |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
39 "Wasn't able to initialize cache."); |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
40 return; |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
41 } |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
42 else { |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
43 String configFile = config.getCacheConfiguration(); |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
44 manager = configFile != null |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
45 ? new CacheManager(configFile) |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
46 : new CacheManager(); |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
47 manager.addCache(CACHE_NAME); |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
48 CacheCleaner cc = new CacheCleaner(); |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
49 cc.start(); |
1985d5db0feb
Implemented a global configuration that should be used to initialize the geobackend.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
897
diff
changeset
|
50 } |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 } |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 public QueryExecutor getQueryExecutor() { |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 return new DefaultQueryExceutor() { |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 public Collection<Result> cachedResults(String query) { |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 Cache cache = manager.getCache(CACHE_NAME); |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 Element element = cache.get(query); |
542
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
59 if (log.isDebugEnabled()) { |
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
60 log.debug("found results in SQL cache: " + (element != null)); |
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
61 } |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 return element != null |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 ? (Collection<Result>)element.getObjectValue() |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 : null; |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 } |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 |
542
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
67 public void cacheResults(String query, Collection<Result> results) { |
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
68 log.debug("store results in SQL cache"); |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 Cache cache = manager.getCache(CACHE_NAME); |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 cache.put(new Element(query, results)); |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 } |
897
02cd2935b5fa
Removed trailing whitespace.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
895
diff
changeset
|
72 |
895
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
73 public void clearCache(String[] tableNames) { |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
74 Cache cache = manager.getCache(CACHE_NAME); |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
75 Iterator keys = cache.getKeys().iterator(); |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
76 while (keys.hasNext()){ |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
77 String origKey = (String)keys.next(); |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
78 String key = origKey.toUpperCase(); |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
79 log.debug(key); |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
80 for (int i = 0; i < tableNames.length; i++){ |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
81 if (key.contains(tableNames[i])){ |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
82 log.debug(tableNames[i]+ " is contained in "+key); |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
83 log.debug("Cacheentry will be removed"); |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
84 boolean success = cache.remove(origKey); |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
85 if (!success){ |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
86 log.warn("Object could not be reoved from Cache."); |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
87 } |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
88 break; |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
89 } |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
90 } |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
91 } |
eb777022b628
Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary
Tim Englich <tim.englich@intevation.de>
parents:
887
diff
changeset
|
92 } |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 }; |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 } |
542
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
95 |
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
96 public void shutdown() { |
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
97 log.info("shutting down SQL cache"); |
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
98 manager.getCache(CACHE_NAME).flush(); |
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
99 manager.shutdown(); |
f0b6d0e2a0f6
Small fixes for Ehcache based SQL results caching.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
541
diff
changeset
|
100 } |
541
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 } |
3cbf11c67fdc
Experimental caching of SQL results via Ehache
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |