Example usage for com.google.common.cache CacheBuilder from

List of usage examples for com.google.common.cache CacheBuilder from

Introduction

In this page you can find the example usage for com.google.common.cache CacheBuilder from.

Prototype

@GwtIncompatible("To be supported")
public static CacheBuilder<Object, Object> from(String spec) 

Source Link

Document

Constructs a new CacheBuilder instance with the settings specified in spec .

Usage

From source file:eu.eubrazilcc.lvl.storage.security.shiro.cache.GuavaCacheManager.java

protected CacheBuilder<Object, Object> getCacheBuilder() {
    if (cacheBuilder == null) {
        synchronized (CacheBuilder.class) {
            if (cacheBuilder == null) {
                if (cacheBuilderSpecification == null) {
                    LOGGER.info("creating default cache builder");
                    cacheBuilder = CacheBuilder.newBuilder().maximumSize(MAX_CACHED_ELEMENTS);
                } else {
                    LOGGER.info("creating cache builder using spec " + cacheBuilderSpecification);
                    cacheBuilder = CacheBuilder.from(cacheBuilderSpecification);
                }//ww  w .j  a va  2  s.com
            }
        }
    }
    return cacheBuilder;
}

From source file:com.pinterest.deployservice.handler.PingHandler.java

public PingHandler(ServiceContext serviceContext) {
    agentDAO = serviceContext.getAgentDAO();
    agentErrorDAO = serviceContext.getAgentErrorDAO();
    deployDAO = serviceContext.getDeployDAO();
    buildDAO = serviceContext.getBuildDAO();
    environDAO = serviceContext.getEnvironDAO();
    hostDAO = serviceContext.getHostDAO();
    utilDAO = serviceContext.getUtilDAO();
    dataHandler = new DataHandler(serviceContext);

    if (serviceContext.isBuildCacheEnabled()) {
        buildCache = CacheBuilder.from(serviceContext.getBuildCacheSpec().replace(";", ","))
                .build(new CacheLoader<String, BuildBean>() {
                    @Override//from w ww.ja va 2s  . c om
                    public BuildBean load(String buildId) throws Exception {
                        return buildDAO.getById(buildId);
                    }
                });
    }

    if (serviceContext.isDeployCacheEnabled()) {
        deployCache = CacheBuilder.from(serviceContext.getDeployCacheSpec().replace(";", ","))
                .build(new CacheLoader<String, DeployBean>() {
                    @Override
                    public DeployBean load(String deployId) throws Exception {
                        return deployDAO.getById(deployId);
                    }
                });
    }
}

From source file:eionet.webq.cache.GuavaCacheManager.java

/**
 * Set the Guava cache specification String to use for building each
 * individual {@link GuavaCache} instance. The given value needs to comply
 * with Guava's {@link CacheBuilderSpec} (see its javadoc).
 *
 * @see #createNativeGuavaCache/*from  w w w  . ja va 2s.co m*/
 * @see com.google.common.cache.CacheBuilder#from(String)
 */
public void setCacheSpecification(String cacheSpecification) {
    this.cacheBuilder = CacheBuilder.from(cacheSpecification);
}

From source file:org.grouplens.lenskit.data.sql.JDBCRatingDAO.java

/**
 * Create a new JDBC rating DAO.  The resulting DAO will be uncached.
 *
 * @param dbc   The database connection.
 * @param sfac  The statement factory./*from w  ww  .  j ava 2s .  c  o  m*/
 * @param close Whether to close the database connection when the DAO is closed.
 * @deprecated Use {@link #newBuilder()}.
 */
@Deprecated
public JDBCRatingDAO(Connection dbc, SQLStatementFactory sfac, boolean close) {
    this(dbc, sfac, close, CacheBuilder.from(CacheBuilderSpec.disableCaching()).<QueryKey, Object>build());
}

From source file:org.springframework.cache.guava.GuavaCacheManager.java

/**
 * Set the Guava cache specification String to use for building each
 * individual {@link GuavaCache} instance. The given value needs to
 * comply with Guava's {@link CacheBuilderSpec} (see its javadoc).
 * @see #createNativeGuavaCache//from   w ww .  ja va  2s  .c  o m
 * @see com.google.common.cache.CacheBuilder#from(String)
 */
public void setCacheSpecification(String cacheSpecification) {
    doSetCacheBuilder(CacheBuilder.from(cacheSpecification));
}

From source file:com.steeleforge.aem.ironsites.cache.service.impl.SimpleCacheServiceImpl.java

private Cache<Object, Object> buildCache(String spec, boolean stats) {
    CacheBuilder<Object, Object> builder = CacheBuilder.from(spec);
    if (stats) {//from   ww  w  . ja v a2 s  .c  o  m
        builder = builder.recordStats();
    }
    return builder.build();
}

From source file:net.opentsdb.catalog.cache.TSMetaCache.java

/**
 * Creates a new TSMetaCache/*  w  w  w.  j  a v  a  2  s  . c om*/
 * @param sqlWorker A SQLWorker to execute lookups and inserts
 * @param metaReader The meta-reader to build the TSMeta from a resultset
 */
public TSMetaCache(SQLWorker sqlWorker, MetaReader metaReader) {
    this.sqlWorker = sqlWorker;
    this.metaReader = metaReader;
    final long maxSize = ConfigurationHelper.getLongSystemThenEnvProperty(MAX_SIZE_PROP, DEFAULT_MAX_SIZE);
    final int concurrency = ConfigurationHelper.getIntSystemThenEnvProperty(CONCURRENCY_PROP,
            DEFAULT_CONCURRENCY);
    final boolean stats = ConfigurationHelper.getBooleanSystemThenEnvProperty(STATS_ENABLED_PROP,
            DEFAULT_STATS_ENABLED);
    final long initialCount = sqlWorker.sqlForLong(COUNT_SQL);
    final long initialSize = (initialCount > maxSize) ? maxSize : initialCount;
    final String spec = String.format(stats ? SPEC_TEMPLATE_WSTATS : SPEC_TEMPLATE_NOSTATS, concurrency,
            initialSize, maxSize);
    cache = CacheBuilder.from(spec).build();
    if (stats) {
        objectName = JMXHelper.objectName(
                new StringBuilder(getClass().getPackage().getName()).append(":service=TSMetaCache"));
        JMXHelper.registerMBean(objectName, new CacheStatistics(cache, objectName));
    } else {
        objectName = null;
    }
    put(metaReader.readTSMetas(sqlWorker.executeQuery(INITIAL_LOAD_SQL, false, initialSize), true)
            .toArray(new TSMeta[0]));
}

From source file:net.opentsdb.catalog.cache.TagPredicateCache.java

/**
 * Creates a new TagPredicateCache/*from w ww  .j  a v a 2 s  . c  om*/
 * @param sqlWorker A SQLWorker to execute lookups and inserts
 */
public TagPredicateCache(SQLWorker sqlWorker) {
    this.sqlWorker = sqlWorker;
    final long maxSize = ConfigurationHelper.getLongSystemThenEnvProperty(MAX_SIZE_PROP, DEFAULT_MAX_SIZE);
    final int concurrency = ConfigurationHelper.getIntSystemThenEnvProperty(CONCURRENCY_PROP,
            DEFAULT_CONCURRENCY);
    final boolean stats = ConfigurationHelper.getBooleanSystemThenEnvProperty(STATS_ENABLED_PROP,
            DEFAULT_STATS_ENABLED);
    final String spec = String.format(stats ? SPEC_TEMPLATE_WSTATS : SPEC_TEMPLATE_NOSTATS, concurrency, 100,
            maxSize);
    cache = CacheBuilder.from(spec).build();
    if (stats) {
        objectName = JMXHelper.objectName(
                new StringBuilder(getClass().getPackage().getName()).append(":service=TagPredicateCache"));
        JMXHelper.registerMBean(objectName, new CacheStatistics(cache, objectName));
    } else {
        objectName = null;
    }
}

From source file:com.redhat.lightblue.savedsearch.SavedSearchCache.java

private void initializeCache(String spec) {
    if (spec == null) {
        cache = CacheBuilder.newBuilder().maximumSize(2048).expireAfterAccess(2, TimeUnit.MINUTES).softValues()
                .build();//  w  w w  .jav a  2 s. c om
    } else {
        cache = CacheBuilder.from(spec).build();
    }
}

From source file:net.opentsdb.catalog.cache.UIDCache.java

/**
 * Creates a new UIDCache/*from   w  w w .  ja  va 2s  .c o m*/
 * @param uidType The UIDMeta type this cache is for
 * @param sqlWorker A SQLWorker to execute lookups and inserts
 * @param metaReader The meta-reader to build the UIDMeta from a resultset
 */
public UIDCache(UniqueId.UniqueIdType uidType, SQLWorker sqlWorker, MetaReader metaReader) {
    this.uidType = uidType;
    this.sqlWorker = sqlWorker;
    this.metaReader = metaReader;
    loadSql = String.format(LOAD_SQL, uidType.name());
    final String name = uidType.name().toLowerCase();
    final long maxSize = ConfigurationHelper.getLongSystemThenEnvProperty(String.format(MAX_SIZE_PROP, name),
            DEFAULT_MAX_SIZE);
    final int concurrency = ConfigurationHelper
            .getIntSystemThenEnvProperty(String.format(CONCURRENCY_PROP, name), DEFAULT_CONCURRENCY);
    final boolean stats = ConfigurationHelper
            .getBooleanSystemThenEnvProperty(String.format(STATS_ENABLED_PROP, name), DEFAULT_STATS_ENABLED);
    final long initialCount = sqlWorker.sqlForLong(String.format(COUNT_SQL, uidType.name()));
    final long initialSize = (initialCount > maxSize) ? maxSize : initialCount;
    final String spec = String.format(stats ? SPEC_TEMPLATE_WSTATS : SPEC_TEMPLATE_NOSTATS, concurrency,
            initialSize, maxSize);
    cache = CacheBuilder.from(spec).build();
    if (stats) {
        objectName = JMXHelper.objectName(new StringBuilder(getClass().getPackage().getName())
                .append(":service=UIDCache,type=").append(uidType.name()));
        JMXHelper.registerMBean(objectName, new CacheStatistics(cache, objectName));
    } else {
        objectName = null;
    }
    put(metaReader.readUIDMetas(
            sqlWorker.executeQuery(String.format(INITIAL_LOAD_SQL, uidType.name()), false, initialSize),
            uidType).toArray(new UIDMeta[0]));
}