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

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

Introduction

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

Prototype

long maximumSize

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

Click Source Link

Usage

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  ww w . j  a v  a  2s.c  o  m*/
    } 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: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  a  v  a2  s . c om*/
    cacheBuilder.initialCapacity(maxCacheSize);
    cacheBuilder.maximumSize(maxCacheSize);
    Cache<T, Object> localCache = cacheBuilder.build();
    this.cache = localCache.asMap();
}

From source file:com.github.fge.jsonschema.core.processing.CachingProcessor.java

/**
 * Main constructor/*from w w w .  j  a  v a 2 s .com*/
 *
 * @param processor the processor
 * @param equivalence an equivalence to use for cache keys
 * @param cacheSize the size of the cache, zero disables it
 * @throws NullPointerException processor or equivalence
 */
public CachingProcessor(final Processor<IN, OUT> processor, final Equivalence<IN> equivalence,
        final int cacheSize) {
    BUNDLE.checkNotNull(processor, "processing.nullProcessor");
    BUNDLE.checkNotNull(equivalence, "processing.nullEquivalence");
    BUNDLE.checkArgument(cacheSize >= -1, "processing.invalidCacheSize");
    this.processor = processor;
    this.equivalence = equivalence;
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if (cacheSize != -1) {
        builder.maximumSize(cacheSize);
    }
    cache = builder.build(loader());
}

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());
    }/*w w  w  . ja  v a 2 s .co  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.metadata.SimpleCache.java

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

From source file:io.datty.unit.UnitSet.java

protected UnitSet(UnitDattyManager parent, String setName, Properties props) {
    this.parent = parent;
    this.name = setName;
    this.props = props;

    String ttlSeconds = props.getProperty(UnitPropertyKeys.MAX_ENTRIES);
    String maxEntries = props.getProperty(UnitPropertyKeys.MAX_ENTRIES);
    if (maxEntries != null) {
        CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
        try {/*w w  w .j  a va2  s.co m*/
            builder.maximumSize(Integer.parseInt(maxEntries));
        } catch (NumberFormatException e) {
            throw new DattyException(
                    "invalid property maxEntries in set: " + setName + ", value=" + maxEntries);
        }
        if (ttlSeconds != null) {
            try {
                builder.expireAfterWrite(Integer.parseInt(ttlSeconds), TimeUnit.SECONDS);
            } catch (NumberFormatException e) {
                throw new DattyException(
                        "invalid property maxEntries in cache: " + setName + ", value=" + maxEntries);
            }
        }

        this.backingCache = builder.build();
        this.recordMap = backingCache.asMap();
    } else {
        this.backingCache = null;
        this.recordMap = new ConcurrentHashMap<String, UnitRecord>();
    }

}

From source file:org.elasticsearch.index.cache.field.data.resident.ResidentFieldDataCache.java

@Override
protected Cache<String, FieldData> buildFieldDataMap() {
    CacheBuilder<String, FieldData> cacheBuilder = CacheBuilder.newBuilder().removalListener(this);
    if (maxSize != -1) {
        cacheBuilder.maximumSize(maxSize);
    }/*  ww w  .ja  v  a 2s .c om*/
    if (expire != null) {
        cacheBuilder.expireAfterAccess(expire.nanos(), TimeUnit.NANOSECONDS);
    }
    return cacheBuilder.build();
}

From source file:com.datatorrent.lib.db.cache.CacheStore.java

@Override
public void connect() throws IOException {
    open = true;/*from  w  w  w. j ava2  s.  c om*/

    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.maximumSize(maxCacheSize);
    if (entryExpiryStrategy == ExpiryType.EXPIRE_AFTER_ACCESS) {
        cacheBuilder.expireAfterAccess(entryExpiryDurationInMillis, TimeUnit.MILLISECONDS);
    } else if (entryExpiryStrategy == ExpiryType.EXPIRE_AFTER_WRITE) {
        cacheBuilder.expireAfterWrite(entryExpiryDurationInMillis, TimeUnit.MILLISECONDS);
    }
    cache = cacheBuilder.build();
    this.cleanupScheduler = Executors.newScheduledThreadPool(1);
    cleanupScheduler.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            cache.cleanUp();
        }
    }, cacheCleanupIntervalInMillis, cacheCleanupIntervalInMillis, TimeUnit.MILLISECONDS);
}

From source file:com.github.pagehelper.cache.GuavaCache.java

public GuavaCache(Properties properties, String prefix) {
    CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
    String maximumSize = properties.getProperty(prefix + ".maximumSize");
    if (StringUtil.isNotEmpty(maximumSize)) {
        cacheBuilder.maximumSize(Long.parseLong(maximumSize));
    } else {/*  ww w .  j  av  a 2  s  . c  o m*/
        cacheBuilder.maximumSize(1000);
    }
    String expireAfterAccess = properties.getProperty(prefix + ".expireAfterAccess");
    if (StringUtil.isNotEmpty(expireAfterAccess)) {
        cacheBuilder.expireAfterAccess(Long.parseLong(expireAfterAccess), TimeUnit.MILLISECONDS);
    }
    String expireAfterWrite = properties.getProperty(prefix + ".expireAfterWrite");
    if (StringUtil.isNotEmpty(expireAfterWrite)) {
        cacheBuilder.expireAfterWrite(Long.parseLong(expireAfterWrite), TimeUnit.MILLISECONDS);
    }
    String initialCapacity = properties.getProperty(prefix + ".initialCapacity");
    if (StringUtil.isNotEmpty(initialCapacity)) {
        cacheBuilder.initialCapacity(Integer.parseInt(initialCapacity));
    }
    CACHE = cacheBuilder.build();
}

From source file:com.sinotopia.mybatis.pagehelper.cache.GuavaCache.java

public GuavaCache(Properties properties, String prefix) {

    CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
    String maximumSize = properties.getProperty(prefix + ".maximumSize");
    if (StringUtil.isNotEmpty(maximumSize)) {
        cacheBuilder.maximumSize(Long.parseLong(maximumSize));
    } else {//  w  w w .java  2  s  . c o m
        cacheBuilder.maximumSize(1000);
    }
    String expireAfterAccess = properties.getProperty(prefix + ".expireAfterAccess");
    if (StringUtil.isNotEmpty(expireAfterAccess)) {
        cacheBuilder.expireAfterAccess(Long.parseLong(expireAfterAccess), TimeUnit.MILLISECONDS);
    }
    String expireAfterWrite = properties.getProperty(prefix + ".expireAfterWrite");
    if (StringUtil.isNotEmpty(expireAfterWrite)) {
        cacheBuilder.expireAfterWrite(Long.parseLong(expireAfterWrite), TimeUnit.MILLISECONDS);
    }
    String initialCapacity = properties.getProperty(prefix + ".initialCapacity");
    if (StringUtil.isNotEmpty(initialCapacity)) {
        cacheBuilder.initialCapacity(Integer.parseInt(initialCapacity));
    }
    CACHE = cacheBuilder.build();
}