view geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/CachingQueryExecutorFactory.java @ 1128:81f0a5e66d71

Add module for FLYS artifacts geo-backend/trunk@1283 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 01 Feb 2011 17:37:09 +0000
parents ebeb56428409
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:

http://dive4elements.wald.intevation.org