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

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

Introduction

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

Prototype

int initialCapacity

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

Click Source Link

Usage

From source file:no.digipost.cache.inmemory.CacheConfig.java

public static CacheConfig initialCapacity(final int initCapacity) {
    return new CacheConfig() {
        @Override//w ww.j  av  a  2 s .  com
        public CacheBuilder<Object, Object> configure(CacheBuilder<Object, Object> builder) {
            LOG.info("Initial capacity = {}", initCapacity);
            return builder.initialCapacity(initCapacity);
        }
    };
}

From source file:com.davidbracewell.cache.impl.GuavaCacheUtils.java

/**
 * Converts a CacheSpec into a CacheBuilder
 *
 * @param specification The specification
 * @param <K>           The key type
 * @param <V>           The value type
 * @return The CacheBuilder/*from  ww w.ja v a2  s .  c om*/
 */
public static <K, V> CacheBuilder<K, V> cacheBuilderFromSpec(CacheSpec<K, V> specification) {
    CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
    if (specification.getMaxSize() > 0) {
        cacheBuilder.maximumSize(specification.getMaxSize());
    }
    if (specification.getConcurrencyLevel() > 0) {
        cacheBuilder.concurrencyLevel(specification.getMaxSize());
    }
    if (specification.getInitialCapacity() > 0) {
        cacheBuilder.initialCapacity(specification.getInitialCapacity());
    }
    if (specification.getExpiresAfterAccess() > 0) {
        cacheBuilder.expireAfterAccess(specification.getExpiresAfterAccess(), TimeUnit.MILLISECONDS);
    }
    if (specification.getExpiresAfterWrite() > 0) {
        cacheBuilder.expireAfterWrite(specification.getExpiresAfterWrite(), TimeUnit.MILLISECONDS);
    }
    if (specification.isWeakKeys()) {
        cacheBuilder.weakKeys();
    }
    if (specification.isWeakValues()) {
        cacheBuilder.weakValues();
    }
    return Cast.as(cacheBuilder);
}

From source file:org.locationtech.geogig.storage.cache.SharedCache.java

@VisibleForTesting
static SharedCache build(int L1capacity, long maxCacheSizeBytes) {
    checkArgument(L1capacity >= 0);/*from w  w  w  .  j a v a 2s .  c o m*/
    checkArgument(maxCacheSizeBytes >= 0, "Cache size can't be < 0, 0 meaning no cache at all");

    if (0L == maxCacheSizeBytes) {
        return NO_CACHE;
    }

    int initialCapacityCount = 1_000_000;
    int concurrencyLevel = 16;

    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder = cacheBuilder.maximumWeight(maxCacheSizeBytes);
    cacheBuilder.weigher(Impl.SizeTracker.WEIGHER);

    cacheBuilder.initialCapacity(initialCapacityCount);
    cacheBuilder.concurrencyLevel(concurrencyLevel);
    cacheBuilder.recordStats();

    Impl.SizeTracker sizeTracker = new Impl.SizeTracker();
    cacheBuilder.removalListener(sizeTracker);

    Cache<Key, byte[]> byteCache = cacheBuilder.build();

    return new Impl(L1capacity, byteCache, sizeTracker);
}

From source file:org.locationtech.geogig.storage.postgresql.PGCache.java

private static PGCache build(Integer initialCapacityCount, Integer concurrencyLevel2, Long maxWeightBytes,
        Optional<Integer> expireSeconds) {
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder = cacheBuilder.maximumWeight(maxWeightBytes);
    cacheBuilder.weigher(weigher);/*  w  ww. j  a  v  a2  s  .co  m*/
    if (expireSeconds.isPresent()) {
        long seconds = expireSeconds.get();
        Preconditions.checkArgument(seconds > -1);
        cacheBuilder.expireAfterAccess(seconds, TimeUnit.SECONDS);
    }
    cacheBuilder.initialCapacity(initialCapacityCount);
    cacheBuilder.concurrencyLevel(concurrencyLevel2);
    cacheBuilder.recordStats();

    SizeTracker sizeTracker = new SizeTracker();
    cacheBuilder.removalListener(sizeTracker);

    Cache<ObjectId, byte[]> byteCache = cacheBuilder.build();

    return new PGCache(byteCache, sizeTracker);
}

From source file:com.github.benmanes.caffeine.cache.testing.GuavaLocalCache.java

/** Returns a Guava-backed cache. */
@SuppressWarnings("CheckReturnValue")
public static <K, V> Cache<K, V> newGuavaCache(CacheContext context) {
    checkState(!context.isAsync(), "Guava caches are synchronous only");

    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if (context.initialCapacity != InitialCapacity.DEFAULT) {
        builder.initialCapacity(context.initialCapacity.size());
    }//from ww w.ja  va2  s. c om
    if (context.isRecordingStats()) {
        builder.recordStats();
    }
    if (context.maximumSize != MaximumSize.DISABLED) {
        if (context.weigher == CacheWeigher.DEFAULT) {
            builder.maximumSize(context.maximumSize.max());
        } else {
            builder.weigher(new GuavaWeigher<Object, Object>(context.weigher));
            builder.maximumWeight(context.maximumWeight());
        }
    }
    if (context.afterAccess != Expire.DISABLED) {
        builder.expireAfterAccess(context.afterAccess.timeNanos(), TimeUnit.NANOSECONDS);
    }
    if (context.afterWrite != Expire.DISABLED) {
        builder.expireAfterWrite(context.afterWrite.timeNanos(), TimeUnit.NANOSECONDS);
    }
    if (context.refresh != Expire.DISABLED) {
        builder.refreshAfterWrite(context.refresh.timeNanos(), TimeUnit.NANOSECONDS);
    }
    if (context.expires() || context.refreshes()) {
        builder.ticker(context.ticker());
    }
    if (context.keyStrength == ReferenceType.WEAK) {
        builder.weakKeys();
    } else if (context.keyStrength == ReferenceType.SOFT) {
        throw new IllegalStateException();
    }
    if (context.valueStrength == ReferenceType.WEAK) {
        builder.weakValues();
    } else if (context.valueStrength == ReferenceType.SOFT) {
        builder.softValues();
    }
    if (context.removalListenerType != Listener.DEFAULT) {
        boolean translateZeroExpire = (context.afterAccess == Expire.IMMEDIATELY)
                || (context.afterWrite == Expire.IMMEDIATELY);
        builder.removalListener(new GuavaRemovalListener<>(translateZeroExpire, context.removalListener));
    }
    Ticker ticker = (context.ticker == null) ? Ticker.systemTicker() : context.ticker;
    if (context.loader == null) {
        context.cache = new GuavaCache<>(builder.<Integer, Integer>build(), ticker);
    } else if (context.loader().isBulk()) {
        context.cache = new GuavaLoadingCache<>(
                builder.build(new BulkLoader<Integer, Integer>(context.loader())), ticker);
    } else {
        context.cache = new GuavaLoadingCache<>(
                builder.build(new SingleLoader<Integer, Integer>(context.loader())), ticker);
    }
    @SuppressWarnings("unchecked")
    Cache<K, V> castedCache = (Cache<K, V>) context.cache;
    return castedCache;
}

From source file:com.github.benmanes.caffeine.cache.testing.GuavaCacheFromContext.java

/** Returns a Guava-backed cache. */
@SuppressWarnings("CheckReturnValue")
public static <K, V> Cache<K, V> newGuavaCache(CacheContext context) {
    checkState(!context.isAsync(), "Guava caches are synchronous only");

    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    context.guava = builder;/*ww w.  ja v  a 2s  .c  om*/

    builder.concurrencyLevel(1);
    if (context.initialCapacity != InitialCapacity.DEFAULT) {
        builder.initialCapacity(context.initialCapacity.size());
    }
    if (context.isRecordingStats()) {
        builder.recordStats();
    }
    if (context.maximumSize != Maximum.DISABLED) {
        if (context.weigher == CacheWeigher.DEFAULT) {
            builder.maximumSize(context.maximumSize.max());
        } else {
            builder.weigher(new GuavaWeigher<Object, Object>(context.weigher));
            builder.maximumWeight(context.maximumWeight());
        }
    }
    if (context.afterAccess != Expire.DISABLED) {
        builder.expireAfterAccess(context.afterAccess.timeNanos(), TimeUnit.NANOSECONDS);
    }
    if (context.afterWrite != Expire.DISABLED) {
        builder.expireAfterWrite(context.afterWrite.timeNanos(), TimeUnit.NANOSECONDS);
    }
    if (context.refresh != Expire.DISABLED) {
        builder.refreshAfterWrite(context.refresh.timeNanos(), TimeUnit.NANOSECONDS);
    }
    if (context.expires() || context.refreshes()) {
        builder.ticker(context.ticker());
    }
    if (context.keyStrength == ReferenceType.WEAK) {
        builder.weakKeys();
    } else if (context.keyStrength == ReferenceType.SOFT) {
        throw new IllegalStateException();
    }
    if (context.valueStrength == ReferenceType.WEAK) {
        builder.weakValues();
    } else if (context.valueStrength == ReferenceType.SOFT) {
        builder.softValues();
    }
    if (context.removalListenerType != Listener.DEFAULT) {
        boolean translateZeroExpire = (context.afterAccess == Expire.IMMEDIATELY)
                || (context.afterWrite == Expire.IMMEDIATELY);
        builder.removalListener(new GuavaRemovalListener<>(translateZeroExpire, context.removalListener));
    }
    Ticker ticker = (context.ticker == null) ? Ticker.systemTicker() : context.ticker;
    if (context.loader == null) {
        context.cache = new GuavaCache<>(builder.<Integer, Integer>build(), ticker, context.isRecordingStats());
    } else if (context.loader().isBulk()) {
        context.cache = new GuavaLoadingCache<>(
                builder.build(new BulkLoader<Integer, Integer>(context.loader())), ticker,
                context.isRecordingStats());
    } else {
        context.cache = new GuavaLoadingCache<>(
                builder.build(new SingleLoader<Integer, Integer>(context.loader())), ticker,
                context.isRecordingStats());
    }
    @SuppressWarnings("unchecked")
    Cache<K, V> castedCache = (Cache<K, V>) context.cache;
    return castedCache;
}

From source file:com.baidu.oped.apm.profiler.metadata.LRUCache.java

public LRUCache(int maxCacheSize) {
    final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.concurrencyLevel(32);/*from  w ww. j av  a 2  s  .  com*/
    cacheBuilder.initialCapacity(maxCacheSize);
    cacheBuilder.maximumSize(maxCacheSize);
    Cache<T, Object> localCache = cacheBuilder.build();
    this.cache = localCache.asMap();
}

From source file:com.navercorp.pinpoint.profiler.metadata.SimpleCache.java

private ConcurrentMap<T, Result> createCache(int maxCacheSize) {
    final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.concurrencyLevel(64);//from  www.j  av a  2 s  .  co m
    cacheBuilder.initialCapacity(maxCacheSize);
    cacheBuilder.maximumSize(maxCacheSize);
    Cache<T, Result> localCache = cacheBuilder.build();
    ConcurrentMap<T, Result> cache = localCache.asMap();
    return cache;
}

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

private ConcurrentMap<Long, ActiveTrace> createCache(int maxActiveTraceSize) {
    final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.concurrencyLevel(64);/*from   w  w  w.j  av  a2s  .c o m*/
    cacheBuilder.initialCapacity(maxActiveTraceSize);
    cacheBuilder.maximumSize(maxActiveTraceSize);
    // OOM defense
    cacheBuilder.weakValues();

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

From source file:org.eclipse.osee.cache.admin.internal.CacheFactory.java

private CacheBuilder<Object, Object> createCacheBuilder(CacheConfiguration config) {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if (config.hasInitialCapacity()) {
        builder = builder.initialCapacity(config.getInitialCapacity());
    }/*from w  w  w .  ja v a 2 s  .com*/
    if (config.hasMaximumSize()) {
        builder = builder.maximumSize(config.getMaximumSize());
    }
    if (config.isExpireAfterAccess()) {
        Pair<Long, TimeUnit> data = config.getExpireAfterAccess();
        builder = builder.expireAfterAccess(data.getFirst(), data.getSecond());
    }
    if (config.isExpireAfterWrite()) {
        Pair<Long, TimeUnit> data = config.getExpireAfterWrite();
        builder = builder.expireAfterWrite(data.getFirst(), data.getSecond());
    }
    if (config.isRefreshAfterWrite()) {
        Pair<Long, TimeUnit> data = config.getRefreshAfterWrite();
        builder = builder.refreshAfterWrite(data.getFirst(), data.getSecond());
    }
    return builder;
}