Example usage for org.apache.commons.jcs.engine.control CompositeCacheManager configure

List of usage examples for org.apache.commons.jcs.engine.control CompositeCacheManager configure

Introduction

In this page you can find the example usage for org.apache.commons.jcs.engine.control CompositeCacheManager configure.

Prototype

public void configure(Properties props) 

Source Link

Document

Configure from properties object.

Usage

From source file:org.openstreetmap.josm.data.cache.JCSCacheManager.java

@SuppressWarnings("resource")
private static void initialize() throws IOException {
    File cacheDir = new File(Main.pref.getCacheDirectory(), "jcs");

    if (!cacheDir.exists() && !cacheDir.mkdirs())
        throw new IOException("Cannot access cache directory");

    File cacheDirLockPath = new File(cacheDir, ".lock");
    if (!cacheDirLockPath.exists() && !cacheDirLockPath.createNewFile()) {
        LOG.log(Level.WARNING, "Cannot create cache dir lock file");
    }// w  w w  .j  a  v a 2s.co  m
    cacheDirLock = new FileOutputStream(cacheDirLockPath).getChannel().tryLock();

    if (cacheDirLock == null)
        LOG.log(Level.WARNING, "Cannot lock cache directory. Will not use disk cache");

    // raising logging level gives ~500x performance gain
    // http://westsworld.dk/blog/2008/01/jcs-and-performance/
    final Logger jcsLog = Logger.getLogger("org.apache.commons.jcs");
    jcsLog.setLevel(Level.INFO);
    jcsLog.setUseParentHandlers(false);
    // we need a separate handler from Main's, as we downgrade LEVEL.INFO to DEBUG level
    Arrays.stream(jcsLog.getHandlers()).forEach(jcsLog::removeHandler);
    jcsLog.addHandler(new Handler() {
        final SimpleFormatter formatter = new SimpleFormatter();

        @Override
        public void publish(LogRecord record) {
            String msg = formatter.formatMessage(record);
            if (record.getLevel().intValue() >= Level.SEVERE.intValue()) {
                Logging.error(msg);
            } else if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
                Logging.warn(msg);
                // downgrade INFO level to debug, as JCS is too verbose at INFO level
            } else if (record.getLevel().intValue() >= Level.INFO.intValue()) {
                Logging.debug(msg);
            } else {
                Logging.trace(msg);
            }
        }

        @Override
        public void flush() {
            // nothing to be done on flush
        }

        @Override
        public void close() {
            // nothing to be done on close
        }
    });

    // this could be moved to external file
    Properties props = new Properties();
    // these are default common to all cache regions
    // use of auxiliary cache and sizing of the caches is done with giving proper geCache(...) params
    // CHECKSTYLE.OFF: SingleSpaceSeparator
    props.setProperty("jcs.default.cacheattributes", CompositeCacheAttributes.class.getCanonicalName());
    props.setProperty("jcs.default.cacheattributes.MaxObjects", DEFAULT_MAX_OBJECTS_IN_MEMORY.get().toString());
    props.setProperty("jcs.default.cacheattributes.UseMemoryShrinker", "true");
    props.setProperty("jcs.default.cacheattributes.DiskUsagePatternName", "UPDATE"); // store elements on disk on put
    props.setProperty("jcs.default.elementattributes", CacheEntryAttributes.class.getCanonicalName());
    props.setProperty("jcs.default.elementattributes.IsEternal", "false");
    props.setProperty("jcs.default.elementattributes.MaxLife", Long.toString(maxObjectTTL));
    props.setProperty("jcs.default.elementattributes.IdleTime", Long.toString(maxObjectTTL));
    props.setProperty("jcs.default.elementattributes.IsSpool", "true");
    // CHECKSTYLE.ON: SingleSpaceSeparator
    CompositeCacheManager cm = CompositeCacheManager.getUnconfiguredInstance();
    cm.configure(props);
    cacheManager = cm;
}