Mercurial > dive4elements > gnv-client
view geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java @ 1145:dfe1ac687c7f tip
added tags
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:16:15 +0200 |
parents | 81f0a5e66d71 |
children |
line wrap: on
line source
/* * Copyright (c) 2010 by Intevation GmbH * * This program is free software under the LGPL (>=v2.1) * Read the file LGPL.txt coming with the software for details * or visit http://www.gnu.org/licenses/ if it does not exist. */ package de.intevation.gnv.geobackend.base.query; import de.intevation.gnv.geobackend.base.Result; import de.intevation.gnv.geobackend.base.query.cache.CacheCleaner; import de.intevation.gnv.geobackend.config.Configuration; import java.lang.management.ManagementFactory; import java.util.Collection; import java.util.Iterator; import javax.management.MBeanServer; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sf.ehcache.management.ManagementService; import org.apache.log4j.Logger; /** * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> */ public class CachingQueryExecutorFactory extends QueryExecutorFactory { 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"); init(); } private void init() { Configuration config = Configuration.getInstance(); if (config == null) { log.error("No geobackend configuration found. " + "Wasn't able to initialize cache."); return; } String configFile = config.getCacheConfiguration(); manager = configFile != null ? new CacheManager(configFile) : new CacheManager(); manager.addCache(CACHE_NAME); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ManagementService.registerMBeans( manager, mBeanServer, false, false, false, true); CacheCleaner cc = new CacheCleaner(); cc.start(); } public QueryExecutor getQueryExecutor() { return new DefaultQueryExceutor() { 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 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 clearCache(String[] tableNames) { Cache cache = manager.getCache(CACHE_NAME); Iterator keys = cache.getKeys().iterator(); while (keys.hasNext()){ String origKey = (String)keys.next(); String key = origKey.toUpperCase(); log.debug(key); for (int i = 0; i < tableNames.length; i++){ if (key.contains(tableNames[i])){ log.debug(tableNames[i]+ " is contained in "+key); log.debug("Cacheentry will be removed"); boolean success = cache.remove(origKey); if (!success){ log.warn("Object could not be reoved from Cache."); } break; } } } } }; } 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: