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

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

Introduction

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

Prototype

public static CacheBuilder<Object, Object> newBuilder() 

Source Link

Document

Constructs a new CacheBuilder instance with default settings, including strong keys, strong values, and no automatic eviction of any kind.

Usage

From source file:org.seedstack.shed.GuavaCache.java

private GuavaCache(CacheParameters<K, V> cacheParameters) {
    final Function<? super K, ? extends V> loadingFunction = cacheParameters.getLoadingFunction();
    cache = CacheBuilder.newBuilder().initialCapacity(cacheParameters.getInitialSize())
            .maximumSize(cacheParameters.getMaxSize()).build(new CacheLoader<K, V>() {
                @Override/*from www  .  j av  a  2 s . c  o  m*/
                public V load(K key) throws Exception {
                    return loadingFunction.apply(key);
                }
            });
}

From source file:com.streamsets.pipeline.stage.processor.kv.LookupUtils.java

private static CacheBuilder createBuilder(CacheConfig conf) {
    CacheBuilder cacheBuilder = CacheBuilder.newBuilder();

    if (!conf.enabled) {
        // If cache is disabled, simply set size to zero
        conf.maxSize = 0;//from w w w  .ja  v  a  2s .c  om
    } else if (conf.maxSize == -1) {
        conf.maxSize = Long.MAX_VALUE;
    }

    // recordStats is available only in Guava 12.0 and above, but
    // CDH still uses guava 11.0. Hence the reflection.
    if (LOG.isDebugEnabled()) {
        try {
            Method m = CacheBuilder.class.getMethod("recordStats");
            if (m != null) {
                m.invoke(cacheBuilder);
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            // We're intentionally ignoring any reflection errors as we might be running
            // with old guava on class path.
        }
    }

    // CacheBuilder doesn't support specifying type thus suffers from erasure, so
    // we build it with this if / else logic.
    if (conf.evictionPolicyType == EvictionPolicyType.EXPIRE_AFTER_ACCESS) {
        cacheBuilder.maximumSize(conf.maxSize).expireAfterAccess(conf.expirationTime, conf.timeUnit);
    } else if (conf.evictionPolicyType == EvictionPolicyType.EXPIRE_AFTER_WRITE) {
        cacheBuilder.maximumSize(conf.maxSize).expireAfterWrite(conf.expirationTime, conf.timeUnit);
    } else {
        throw new IllegalArgumentException(
                Utils.format("Unrecognized EvictionPolicyType: '{}'", conf.evictionPolicyType));
    }

    return cacheBuilder;
}

From source file:org.magnum.soda.svc.SessionData.java

private static Cache<String, SessionData> createCache(int maxSize, int expireAfter, TimeUnit unit) {
    return CacheBuilder.newBuilder().maximumSize(maxSize).expireAfterAccess(expireAfter, unit)
            // .removalListener(MY_LISTENER)
            .build(loader_);/*from  w  w  w  . j a va 2 s. c om*/
}

From source file:com.github.benmanes.caffeine.cache.impl.GuavaCache.java

public GuavaCache(int maximumSize) {
    cache = CacheBuilder.newBuilder().concurrencyLevel(CONCURRENCY_LEVEL).initialCapacity(maximumSize)
            .maximumSize(maximumSize).build();
}

From source file:org.glowroot.central.util.RateLimiter.java

public RateLimiter(int maximumSize, boolean recordStats) {
    CacheBuilder<Object, Object> cache = CacheBuilder.newBuilder().expireAfterWrite(1, DAYS);
    if (maximumSize != NO_MAXIMUM_SIZE) {
        cache.maximumSize(maximumSize);// w ww  .ja  v a2s  . c  om
    }
    if (recordStats) {
        cache.recordStats();
    }
    acquiredRecently = cache.build();
}

From source file:com.twitter.aurora.scheduler.stats.CachedCounters.java

@Inject
CachedCounters(final StatsProvider stats) {
    cache = CacheBuilder.newBuilder().build(new CacheLoader<String, AtomicLong>() {
        @Override/*from w w  w  .j  a  va2s. c om*/
        public AtomicLong load(String key) {
            return stats.makeCounter(key);
        }
    });
}

From source file:com.googlecode.webutilities.common.cache.impl.GoogleCache.java

public GoogleCache(CacheConfig<K, V> cacheConfig) {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().softValues();
    // if(reloadAfterAccess > 0)
    //     builder.expireAfterAccess(reloadAfterAccess, TimeUnit.SECONDS);
    if (cacheConfig.getReloadTime() > 0)
        builder.expireAfterWrite(cacheConfig.getReloadTime(), TimeUnit.SECONDS);
    googleCache = builder.build();//from  w w w .  ja  va2 s.  c o  m
}

From source file:org.grouplens.lenskit.eval.traintest.CachingDAOProvider.java

private static void registerProviderClass(Class<?> cls) {
    Cache<EventDAO, Object> cache = CACHE_MAP.get(cls);
    if (cache == null) {
        cache = CacheBuilder.newBuilder().weakKeys().softValues().build();
        CACHE_MAP.putIfAbsent(cls, cache);
    }/*from w w w. j a  v a2  s  .c o  m*/
}

From source file:com.cloudera.nav.plugin.client.writer.registry.AbstractRegistryFactory.java

public LoadingCache<Class<?>, Collection<T>> newRegistry() {
    return CacheBuilder.newBuilder().maximumSize(MAX_CACHE_SIZE)
            .build(new CacheLoader<Class<?>, Collection<T>>() {
                @Override// w  w w  .  j a  v a2 s  .  c  o  m
                public Collection<T> load(Class<?> aClass) throws Exception {
                    return parseMClass(aClass);
                }
            });
}

From source file:org.apache.hadoop.hdfs.server.datanode.web.webhdfs.DataNodeUGIProvider.java

public static synchronized void init(Configuration conf) {
    if (ugiCache == null) {
        ugiCache = CacheBuilder.newBuilder()
                .expireAfterAccess(// w  w  w.  j  a  v  a 2s . c  om
                        conf.getInt(DFSConfigKeys.DFS_WEBHDFS_UGI_EXPIRE_AFTER_ACCESS_KEY,
                                DFSConfigKeys.DFS_WEBHDFS_UGI_EXPIRE_AFTER_ACCESS_DEFAULT),
                        TimeUnit.MILLISECONDS)
                .build();
    }
}