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

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

Introduction

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

Prototype

public <K1 extends K, V1 extends V> Cache<K1, V1> build() 

Source Link

Document

Builds a cache which does not automatically load values when keys are requested.

Usage

From source file:com.heliosapm.jmx.cache.CacheStatistics.java

/**
 * Creates a new CacheStatistics and the underlying cache from the passed {@link CacheBuilder}.
 * This call causes this CacheStatistics to register itself as the cache's removal listener
 * and removal events are broadcast as JMX notifications.
 * @param builder The guava cache builder
 * @param cacheName The assigned name for this cache
 *//* w w w.  j  a va  2 s  .c o  m*/
public CacheStatistics(final CacheBuilder<K, V> builder, final String cacheName) {
    super(SharedNotificationExecutor.getInstance(), notificationInfos);
    builder.removalListener(this);
    this.cache = builder.build();
    this.cacheName = cacheName;
    objectName = JMXHelper.objectName(JMXHelper.objectName("com.heliosapm.cache:name=" + cacheName));
}

From source file:org.surfnet.oaaas.auth.AuthorizationServerFilter.java

@SuppressWarnings({ "rawtypes", "unchecked" })
protected Cache<String, VerifyTokenResponse> buildCache(boolean recordStats) {
    CacheBuilder cacheBuilder = CacheBuilder.newBuilder().maximumSize(100000).expireAfterAccess(10,
            TimeUnit.MINUTES);/*  w w w  .jav  a2 s . co m*/
    return recordStats ? cacheBuilder.recordStats().build() : cacheBuilder.build();
}

From source file:de.openali.odysseus.chart.factory.config.resolver.ChartTypeResolver.java

private ChartTypeResolver() {
    final CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().expireAfterAccess(10,
            TimeUnit.MINUTES);/*ww w. j  a  va 2s.  c o m*/
    m_loaderCache = builder.build();
}

From source file:me.defying.chili.memoize.MemoizeCacheLoader.java

@Override
public Cache<Object, Optional<Object>> load(Method key) throws Exception {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    Memoize annotation = key.getAnnotation(Memoize.class);

    if (annotation.size() > 0) {
        builder.maximumSize(annotation.size());
    }//from  www. j  av a2  s .c o m

    if (annotation.time() > 0) {
        builder.expireAfterWrite(annotation.time(), annotation.unit());
    }

    if (annotation.statistics()) {
        builder.recordStats();
    }

    return builder.build();
}

From source file:com.navercorp.pinpoint.profiler.context.active.DefaultActiveTraceRepository.java

private ConcurrentMap<ActiveTraceHandle, ActiveTrace> createCache(int maxActiveTraceSize) {
    final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.concurrencyLevel(64);//  w w w .  j  a va 2  s . c o m
    cacheBuilder.initialCapacity(maxActiveTraceSize);
    cacheBuilder.maximumSize(maxActiveTraceSize);

    final Cache<ActiveTraceHandle, ActiveTrace> localCache = cacheBuilder.build();
    return localCache.asMap();
}

From source file:org.hypoport.springGuavaCacheAdapter.SpringGuavaCacheAdapter.java

private void createGuavaCache() {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if (maximumSize != null) {
        builder.maximumSize(maximumSize);
    }/*  w  w  w .java  2 s  .  co m*/
    if (expireAfterAccessInSeconds != null) {
        builder.expireAfterAccess(expireAfterAccessInSeconds, SECONDS);
    }
    if (expireAfterWriteInSeconds != null) {
        builder.expireAfterWrite(expireAfterWriteInSeconds, SECONDS);
    }
    cache = builder.build();
}

From source file:com.datatorrent.lib.database.CacheStore.java

public CacheStore(CacheProperties properties) {
    Preconditions.checkNotNull(properties.entryExpiryStrategy, "expiryType");

    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    if (properties.entryExpiryStrategy == ExpiryType.EXPIRE_AFTER_ACCESS) {
        cacheBuilder.expireAfterAccess(properties.entryExpiryDurationInMillis, TimeUnit.MILLISECONDS);
    } else if (properties.entryExpiryStrategy == ExpiryType.EXPIRE_AFTER_WRITE) {
        cacheBuilder.expireAfterWrite(properties.entryExpiryDurationInMillis, TimeUnit.MILLISECONDS);
    }//from w w  w .  j  a v  a  2s . c  om
    cache = cacheBuilder.build();
    this.cleanupScheduler = Executors.newScheduledThreadPool(1);
    cleanupScheduler.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            cache.cleanUp();
        }
    }, properties.cacheCleanupIntervalInMillis, properties.cacheCleanupIntervalInMillis, TimeUnit.MILLISECONDS);
}

From source file:org.locationtech.geogig.di.caching.CacheFactory.java

protected synchronized void createCache() {
    if (cache != null) {
        return;//from  ww  w  . ja v  a 2s. co  m
    }
    if (!cacheIsEnabled()) {
        this.cache = NO_CACHE;
        return;
    }
    final int maxSize = getConfig("maxSize", 50_000);
    final int concurrencyLevel = getConfig("concurrencyLevel", 4);

    final int expireSeconds = getConfig("expireSeconds", 300);
    final int initialCapacity = getConfig("initialCapacity", 10 * 1000);
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder = cacheBuilder.maximumSize(maxSize);
    cacheBuilder.expireAfterAccess(expireSeconds, TimeUnit.SECONDS);
    cacheBuilder.initialCapacity(initialCapacity);
    cacheBuilder.concurrencyLevel(concurrencyLevel);
    cacheBuilder.softValues();

    try {
        this.cache = cacheBuilder.build();
    } catch (RuntimeException e) {
        LOGGER.error(
                "Error configuring cache '{}' with maxSize: {}, expireSeconds: {}, initialCapacity: {}, concurrencyLevel: {}",
                configKeywordPrefix, maxSize, expireSeconds, initialCapacity, concurrencyLevel, e);

        throw e;
    }

    LOGGER.debug(
            "Cache '{}' configured with maxSize: {}, expireSeconds: {}, initialCapacity: {}, concurrencyLevel: {}",
            configKeywordPrefix, maxSize, expireSeconds, initialCapacity, concurrencyLevel);

}

From source file:org.nuxeo.ecm.core.cache.InMemoryCacheImpl.java

public InMemoryCacheImpl(CacheDescriptor desc) {
    super(desc);//from ww w  .  j ava 2  s  .  co m
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    builder = builder.expireAfterWrite(desc.ttl, TimeUnit.MINUTES);
    if (desc.options.containsKey("concurrencyLevel")) {
        builder = builder.concurrencyLevel(Integer.valueOf(desc.options.get("concurrencyLevel")).intValue());
    }
    if (desc.options.containsKey("maxSize")) {
        builder = builder.maximumSize(Integer.valueOf(desc.options.get("maxSize")).intValue());
    }
    cache = builder.build();
}

From source file:com.amazon.janusgraph.diskstorage.dynamodb.AbstractDynamoDBStore.java

public AbstractDynamoDBStore(final DynamoDBStoreManager manager, final String prefix, final String storeName) {
    this.manager = manager;
    this.client = this.manager.client();
    this.storeName = storeName;
    this.tableName = prefix + "_" + storeName;
    this.forceConsistentRead = client.forceConsistentRead();

    final CacheBuilder<Pair<StaticBuffer, StaticBuffer>, DynamoDBStoreTransaction> builder = CacheBuilder
            .newBuilder().concurrencyLevel(client.delegate().getMaxConcurrentUsers())
            .expireAfterWrite(manager.getLockExpiresDuration().toMillis(), TimeUnit.MILLISECONDS)
            .removalListener(ReportingRemovalListener.theInstance());
    this.keyColumnLocalLocks = builder.build();
}