Example usage for org.apache.commons.cache Cache store

List of usage examples for org.apache.commons.cache Cache store

Introduction

In this page you can find the example usage for org.apache.commons.cache Cache store.

Prototype

public boolean store(Serializable key, Serializable val, Long expiry, Long cost);

Source Link

Document

Store the specified val under the specified key.

Usage

From source file:com.sslexplorer.core.CoreUtil.java

/**
 * Store to cache, checking for serializable.
 * /*from  w w w.jav  a 2 s.  c o  m*/
 * @param cache cache to store object in 
 * @param key cache key
 * @param object object to cache
 * @param ttl time-to-live in milliseconds
 * @param cost cost
 */
public static void storeToCache(Cache cache, String key, Serializable object, long ttl, long cost) {
    if (log.isDebugEnabled()) {
        log.debug("Caching under " + key + ", ttl=" + ttl + ", cost=" + cost);
    }

    // NOTE Temporary code to make sure policy objects are serializable, in development and testing
    if ("true".equals(SystemProperties.get("sslexplorer.useDevConfig"))
            | "true".equals(SystemProperties.get("sslexplorer.testing"))) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
        } catch (Exception e) {
            String string = "********** Failed to cache policy database object. There is probably a non-serializable object somewhere in the object graph. PLEASE FIX ME ****************";
            System.err.println(string);
            e.printStackTrace();
            throw new RuntimeException(string);
        }
    }

    cache.store(key, object, new Long(ttl + System.currentTimeMillis()), cost);
    if (log.isDebugEnabled()) {
        log.debug("NUM_RETRIEVE_REQUESTED " + cache.getStat(CacheStat.NUM_RETRIEVE_REQUESTED));
        log.debug("NUM_RETRIEVE_FOUND " + cache.getStat(CacheStat.NUM_RETRIEVE_FOUND));
        log.debug("NUM_RETRIEVE_NOT_FOUND " + cache.getStat(CacheStat.NUM_RETRIEVE_NOT_FOUND));
        log.debug("NUM_STORE_REQUESTED " + cache.getStat(CacheStat.NUM_STORE_REQUESTED));
        log.debug("NUM_STORE_STORED " + cache.getStat(CacheStat.NUM_STORE_STORED));
        log.debug("NUM_STORE_NOT_STORED " + cache.getStat(CacheStat.NUM_STORE_NOT_STORED));
        log.debug("CUR_CAPACITY " + cache.getStat(CacheStat.CUR_CAPACITY));
    }
}

From source file:com.adito.core.CoreUtil.java

/**
 * Store to cache, checking for serializable.
 * //from w  ww  . j a va 2s .c  om
 * @param cache cache to store object in 
 * @param key cache key
 * @param object object to cache
 * @param ttl time-to-live in milliseconds
 * @param cost cost
 */
public static void storeToCache(Cache cache, String key, Serializable object, long ttl, long cost) {
    if (log.isDebugEnabled()) {
        log.debug("Caching under " + key + ", ttl=" + ttl + ", cost=" + cost);
    }

    // NOTE Temporary code to make sure policy objects are serializable, in development and testing
    if ("true".equals(SystemProperties.get("adito.useDevConfig"))
            | "true".equals(SystemProperties.get("adito.testing"))) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
        } catch (Exception e) {
            String string = "********** Failed to cache policy database object. There is probably a non-serializable object somewhere in the object graph. PLEASE FIX ME ****************";
            System.err.println(string);
            e.printStackTrace();
            throw new RuntimeException(string);
        }
    }

    cache.store(key, object, new Long(ttl + System.currentTimeMillis()), cost);
    if (log.isDebugEnabled()) {
        log.debug("NUM_RETRIEVE_REQUESTED " + cache.getStat(CacheStat.NUM_RETRIEVE_REQUESTED));
        log.debug("NUM_RETRIEVE_FOUND " + cache.getStat(CacheStat.NUM_RETRIEVE_FOUND));
        log.debug("NUM_RETRIEVE_NOT_FOUND " + cache.getStat(CacheStat.NUM_RETRIEVE_NOT_FOUND));
        log.debug("NUM_STORE_REQUESTED " + cache.getStat(CacheStat.NUM_STORE_REQUESTED));
        log.debug("NUM_STORE_STORED " + cache.getStat(CacheStat.NUM_STORE_STORED));
        log.debug("NUM_STORE_NOT_STORED " + cache.getStat(CacheStat.NUM_STORE_NOT_STORED));
        log.debug("CUR_CAPACITY " + cache.getStat(CacheStat.CUR_CAPACITY));
    }
}