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

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

Introduction

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

Prototype

public CacheBuilder<K, V> recordStats() 

Source Link

Document

Enable the accumulation of CacheStats during the operation of the cache.

Usage

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

public static CacheConfig recordStats() {
    return new CacheConfig() {
        @Override/*  w ww.j a va2  s  . c o  m*/
        public CacheBuilder<Object, Object> configure(CacheBuilder<Object, Object> builder) {
            LOG.info("Recording stats");
            return builder.recordStats();
        }
    };
}

From source file:org.opendaylight.infrautils.caches.guava.internal.GuavaCacheProvider.java

private static <K, V> CacheBuilder<K, V> newCacheBuilder(BaseCacheConfig cacheConfig,
        CachePolicy initialPolicy) {//from w  ww  . java 2s . c o m
    Objects.requireNonNull(cacheConfig, "cacheConfig");
    @SuppressWarnings("unchecked")
    // TODO support from(String) with CacheBuilderSpec for configuration from CachePolicy's extensions
    CacheBuilder<K, V> builder = (CacheBuilder<K, V>) CacheBuilder.newBuilder();
    // builder.concurrencyLevel(concurrencyLevel) <= this is a Guava specific setting, TBD String CacheBuilderSpec
    if (initialPolicy.statsEnabled()) {
        builder.recordStats();
    }

    long maxEntries = initialPolicy.maxEntries();
    if (maxEntries != CachePolicy.UNLIMITED_ENTRIES) {
        builder.maximumSize(maxEntries);
    }

    // TODO @see CachePolicy
    //   * TODO builder.concurrencyLevel(initialPolicy.?.concurrencyLevel);
    //   * TODO builder.initialCapacity(initialPolicy.?.initialCapacity)
    return builder;
}

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

@VisibleForTesting
static SharedCache build(int L1capacity, long maxCacheSizeBytes) {
    checkArgument(L1capacity >= 0);//from   ww  w  .  j a  v  a  2  s  . 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  va  2 s. c om
    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.sri.ai.expresso.core.DefaultSymbol.java

private static Cache<Object, Symbol> newSymbolTable() {
    CacheBuilder<Object, Object> cb = CacheBuilder.newBuilder();

    long maximumSize = ExpressoConfiguration.getGlobalSymbolTableMaximumSize();
    // Note: a maximumSize of 
    // < 0 means no size restrictions
    // = 0 means no cache
    // > 0 means maximum size of cache
    if (maximumSize >= 0L) {
        cb.maximumSize(maximumSize);//from   w w w  .j  a v  a 2s .com
    }
    if (AICUtilConfiguration.isRecordCacheStatistics()) {
        cb.recordStats();
    }

    Cache<Object, Symbol> result = cb.build();

    return result;
}

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   w w  w .  ja  v a 2  s  .  c  o  m*/
    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;/*from  w  w w  .  jav  a  2s.  c  o  m*/

    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.sri.ai.expresso.core.DefaultSyntaxLeaf.java

private static Cache<Object, SyntaxLeaf> newSymbolTable() {
    CacheBuilder<Object, Object> cb = CacheBuilder.newBuilder();

    long maximumSize = ExpressoConfiguration.getGlobalSymbolTableMaximumSize();
    // Note: a maximumSize of 
    // < 0 means no size restrictions
    // = 0 means no cache
    // > 0 means maximum size of cache
    if (maximumSize >= 0L) {
        cb.maximumSize(maximumSize);//from w  ww .j  a  v a2s. co m
    }
    if (AICUtilConfiguration.isRecordCacheStatistics()) {
        cb.recordStats();
    }

    Cache<Object, SyntaxLeaf> result = cb.build();

    return result;
}

From source file:com.google.gerrit.server.cache.h2.DefaultCacheFactory.java

@SuppressWarnings("unchecked")
<K, V> CacheBuilder<K, V> create(CacheBinding<K, V> def, boolean unwrapValueHolder) {
    CacheBuilder<K, V> builder = newCacheBuilder();
    builder.recordStats();
    builder.maximumWeight(cfg.getLong("cache", def.name(), "memoryLimit", def.maximumWeight()));

    builder.removalListener(forwardingRemovalListenerFactory.create(def.name()));

    Weigher<K, V> weigher = def.weigher();
    if (weigher != null && unwrapValueHolder) {
        final Weigher<K, V> impl = weigher;
        weigher = (Weigher<K, V>) new Weigher<K, ValueHolder<V>>() {
            @Override//from   w  w  w  . j  av a2s  .c  o m
            public int weigh(K key, ValueHolder<V> value) {
                return impl.weigh(key, value.value);
            }
        };
    } else if (weigher == null) {
        weigher = unitWeight();
    }
    builder.weigher(weigher);

    Long age = def.expireAfterWrite(TimeUnit.SECONDS);
    if (has(def.name(), "maxAge")) {
        builder.expireAfterWrite(ConfigUtil.getTimeUnit(cfg, "cache", def.name(), "maxAge",
                age != null ? age : 0, TimeUnit.SECONDS), TimeUnit.SECONDS);
    } else if (age != null) {
        builder.expireAfterWrite(age, TimeUnit.SECONDS);
    }

    return builder;
}

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  w  w w  . j  a v a2  s. c  o m
        builder = builder.recordStats();
    }
    return builder.build();
}