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

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

Introduction

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

Prototype

int concurrencyLevel

To view the source code for com.google.common.cache CacheBuilder concurrencyLevel.

Click Source Link

Usage

From source file:org.eclipse.xtext.xbase.ide.types.ClasspathScanner.java

protected Cache<Pair<ClassLoader, Collection<String>>, Iterable<ITypeDescriptor>> createClassLoaderCache() {
    CacheBuilder<Object, Object> _newBuilder = CacheBuilder.newBuilder();
    CacheBuilder<Object, Object> _initialCapacity = _newBuilder.initialCapacity(8);
    CacheBuilder<Object, Object> _concurrencyLevel = _initialCapacity.concurrencyLevel(2);
    CacheBuilder<Object, Object> _maximumSize = _concurrencyLevel.maximumSize(32);
    CacheBuilder<Object, Object> _expireAfterAccess = _maximumSize.expireAfterAccess(5, TimeUnit.MINUTES);
    return _expireAfterAccess.<Pair<ClassLoader, Collection<String>>, Iterable<ITypeDescriptor>>build();
}

From source file:org.eclipse.xtext.xbase.ide.types.ClasspathScanner.java

protected Cache<Pair<URI, Collection<String>>, Iterable<ITypeDescriptor>> createUriCache() {
    CacheBuilder<Object, Object> _newBuilder = CacheBuilder.newBuilder();
    CacheBuilder<Object, Object> _initialCapacity = _newBuilder.initialCapacity(64);
    CacheBuilder<Object, Object> _concurrencyLevel = _initialCapacity.concurrencyLevel(2);
    CacheBuilder<Object, Object> _maximumSize = _concurrencyLevel.maximumSize(256);
    CacheBuilder<Object, Object> _expireAfterAccess = _maximumSize.expireAfterAccess(30, TimeUnit.MINUTES);
    return _expireAfterAccess.<Pair<URI, Collection<String>>, Iterable<ITypeDescriptor>>build();
}

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

protected synchronized void createCache() {
    if (cache != null) {
        return;//from w  ww.  j  av a  2s. c  om
    }
    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.graylog2.lookup.caches.GuavaLookupCache.java

@Inject
public GuavaLookupCache(@Assisted("id") String id, @Assisted("name") String name,
        @Assisted LookupCacheConfiguration c, @Named("processbuffer_processors") int processorCount,
        MetricRegistry metricRegistry) {
    super(id, name, c, metricRegistry);
    Config config = (Config) c;//ww  w.  jav a 2  s.c o  m
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();

    // the theory is that typically only processors will affect the cache concurrency, whereas decorator usage is less critical
    builder.concurrencyLevel(processorCount).recordStats();

    builder.maximumSize(config.maxSize());
    if (config.expireAfterAccess() > 0 && config.expireAfterAccessUnit() != null) {
        //noinspection ConstantConditions
        builder.expireAfterAccess(config.expireAfterAccess(), config.expireAfterAccessUnit());
    }
    if (config.expireAfterWrite() > 0 && config.expireAfterWriteUnit() != null) {
        //noinspection ConstantConditions
        builder.expireAfterWrite(config.expireAfterWrite(), config.expireAfterWriteUnit());
    }

    cache = new InstrumentedCache<>(builder.build(), this);
}

From source file:org.geogit.di.caching.CacheFactory.java

protected synchronized void createCache() {
    if (cache != null) {
        return;//from w w  w .j a va 2s.  co  m
    }
    if (!cacheIsEnabled()) {
        this.cache = NO_CACHE;
        return;
    }
    final int maxSize = getConfig("maxSize", 50 * 1000);
    final int concurrencyLevel = getConfig("concurrencyLevel", 0);
    if (concurrencyLevel == 0) {
        this.cache = new SimpleCache<ObjectId, RevObject>(maxSize);
        LOGGER.info("Cache '{}' configured with maxSize: {}", configKeywordPrefix, maxSize);
        return;
    }

    final int expireSeconds = getConfig("expireSeconds", 30);
    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);

    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.elasticsearch.indices.cache.filter.IndicesFilterCache.java

private void buildCache() {
    CacheBuilder<WeightedFilterCache.FilterCacheKey, DocIdSet> cacheBuilder = CacheBuilder.newBuilder()
            .removalListener(this).maximumWeight(sizeInBytes)
            .weigher(new WeightedFilterCache.FilterCacheValueWeigher());

    // defaults to 4, but this is a busy map for all indices, increase it a bit
    cacheBuilder.concurrencyLevel(16);

    if (expire != null) {
        cacheBuilder.expireAfterAccess(expire.millis(), TimeUnit.MILLISECONDS);
    }/*  w  w  w .  j  av  a  2  s. com*/

    cache = cacheBuilder.build();
}

From source file:org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache.java

@Inject
public IndicesFieldDataCache(Settings settings, IndicesFieldDataCacheListener indicesFieldDataCacheListener) {
    super(settings);
    this.indicesFieldDataCacheListener = indicesFieldDataCacheListener;
    String size = componentSettings.get("size", "-1");
    long sizeInBytes = componentSettings.getAsMemory("size", "-1").bytes();
    if (sizeInBytes > ByteSizeValue.MAX_GUAVA_CACHE_SIZE.bytes()) {
        logger.warn("reducing requested field data cache size of [{}] to the maximum allowed size of [{}]",
                new ByteSizeValue(sizeInBytes), ByteSizeValue.MAX_GUAVA_CACHE_SIZE);
        sizeInBytes = ByteSizeValue.MAX_GUAVA_CACHE_SIZE.bytes();
        size = ByteSizeValue.MAX_GUAVA_CACHE_SIZE.toString();
    }// w  w  w  . jav a  2 s  . c o  m
    final TimeValue expire = componentSettings.getAsTime("expire", null);
    CacheBuilder<Key, RamUsage> cacheBuilder = CacheBuilder.newBuilder().removalListener(this);
    if (sizeInBytes > 0) {
        cacheBuilder.maximumWeight(sizeInBytes).weigher(new FieldDataWeigher());
    }
    // defaults to 4, but this is a busy map for all indices, increase it a bit
    cacheBuilder.concurrencyLevel(16);
    if (expire != null && expire.millis() > 0) {
        cacheBuilder.expireAfterAccess(expire.millis(), TimeUnit.MILLISECONDS);
    }
    logger.debug("using size [{}] [{}], expire [{}]", size, new ByteSizeValue(sizeInBytes), expire);
    cache = cacheBuilder.build();
}

From source file:org.alfresco.repo.cache.DefaultSimpleCache.java

/**
 * Construct a cache using the specified capacity and name.
 * //from ww w.j a  va2 s. c  o m
 * @param maxItems The cache capacity. 0 = use {@link #DEFAULT_CAPACITY}
 * @param useMaxItems Whether the maxItems value should be applied as a size-cap for the cache.
 * @param cacheName An arbitrary cache name.
 */
@SuppressWarnings("unchecked")
public DefaultSimpleCache(int maxItems, boolean useMaxItems, int ttlSecs, int maxIdleSecs, String cacheName) {
    if (maxItems == 0) {
        maxItems = DEFAULT_CAPACITY;
    } else if (maxItems < 0) {
        throw new IllegalArgumentException("maxItems may not be negative, but was " + maxItems);
    }
    this.maxItems = maxItems;
    this.useMaxItems = useMaxItems;
    this.ttlSecs = ttlSecs;
    this.maxIdleSecs = maxIdleSecs;
    setBeanName(cacheName);

    // The map will have a bounded size determined by the maxItems member variable.
    @SuppressWarnings("rawtypes")
    CacheBuilder builder = CacheBuilder.newBuilder();

    if (useMaxItems) {
        builder.maximumSize(maxItems);
    }
    if (ttlSecs > 0) {
        builder.expireAfterWrite(ttlSecs, TimeUnit.SECONDS);
    }
    if (maxIdleSecs > 0) {
        builder.expireAfterAccess(maxIdleSecs, TimeUnit.SECONDS);
    }
    builder.concurrencyLevel(32);

    cache = (Cache<K, AbstractMap.SimpleImmutableEntry<K, V>>) builder.build();
}

From source file:software.coolstuff.springframework.owncloud.service.impl.rest.OwncloudRestResourceServiceImpl.java

protected LoadingCache<String, Sardine> buildSardineCache() {
    CacheProperties cacheProperties = properties.getResourceService().getSardineCache();
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if (cacheProperties.getConcurrencyLevel() != null) {
        builder.concurrencyLevel(cacheProperties.getConcurrencyLevel());
    }//  www.j  ava2s  .  c  o  m
    if (cacheProperties.getExpireAfterAccess() != null
            && cacheProperties.getExpireAfterAccessTimeUnit() != null) {
        builder.expireAfterAccess(cacheProperties.getExpireAfterAccess(),
                cacheProperties.getExpireAfterAccessTimeUnit());
    }
    if (cacheProperties.getExpireAfterWrite() != null
            && cacheProperties.getExpireAfterWriteTimeUnit() != null) {
        builder.expireAfterWrite(cacheProperties.getExpireAfterWrite(),
                cacheProperties.getExpireAfterWriteTimeUnit());
    }
    if (cacheProperties.getInitialCapacity() != null) {
        builder.initialCapacity(cacheProperties.getInitialCapacity());
    }
    if (cacheProperties.getMaximumSize() != null) {
        builder.maximumSize(cacheProperties.getMaximumSize());
    }
    if (cacheProperties.getMaximumWeight() != null) {
        builder.maximumWeight(cacheProperties.getMaximumWeight());
    }
    if (cacheProperties.getRefreshAfterWrite() != null
            && cacheProperties.getRefreshAfterWriteTimeUnit() != null) {
        builder.refreshAfterWrite(cacheProperties.getRefreshAfterWrite(),
                cacheProperties.getRefreshAfterWriteTimeUnit());
    }
    return builder.build(sardineCacheLoader);
}

From source file:org.nuxeo.ecm.core.storage.dbs.DBSCachingRepository.java

protected <T> Cache<String, T> newCache(DBSRepositoryDescriptor descriptor) {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    builder = builder.expireAfterWrite(descriptor.cacheTTL.longValue(), TimeUnit.MINUTES);
    if (descriptor.cacheConcurrencyLevel != null) {
        builder = builder.concurrencyLevel(descriptor.cacheConcurrencyLevel.intValue());
    }/*  w ww  . j  av  a  2s  .c o  m*/
    if (descriptor.cacheMaxSize != null) {
        builder = builder.maximumSize(descriptor.cacheMaxSize.longValue());
    }
    return builder.build();
}