view geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java @ 1124:f3fdbeef1c68

Made sql-cache statistics available as MBean in JConsole. geo-backend/trunk@1156 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 04 Jun 2010 08:36:58 +0000
parents 1985d5db0feb
children ebeb56428409
line wrap: on
line source
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;
        }
        else {
            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:

http://dive4elements.wald.intevation.org