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:rickbw.incubator.cache.TimedEvictionCacheBuilder.java

/**
 * @throws IllegalStateException    If neither {@link #evictAfterAccess()}
 *              nor {@link #evictAfterWrite()} has been called, or if no
 *              periods have been added with
 *              {@link #period(long, TimeUnit, int)}.
 *///from  ww  w .j  a  v a 2  s  .c  o m
public Cache<K, V> build() {
    if (this.periods.isEmpty()) {
        throw new IllegalStateException("no periods defined");
    } else if (this.periods.size() == 1) {
        final CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
        configureBuilder(builder, this.periods.get(0));
        return builder.build();
    } else {
        assert this.periods.size() > 1;
        final Map<Period, Cache<K, V>> caches = new IdentityHashMap<>();
        for (final Period period : this.periods) {
            final CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
            configureBuilder(builder, period);
            final Cache<K, V> cache = builder.build();
            caches.put(period, cache);
        }
        return new MultiCache<K, V>(this.elementKeyClass, caches, new PeriodSelector<>(this.periods));
    }
}

From source file:com.streamsets.pipeline.lib.parser.net.netflow.v9.NetflowV9Decoder.java

public static Cache<FlowSetTemplateCacheKey, FlowSetTemplate> buildTemplateCache(int maxTemplateCacheSize,
        int templateCacheTimeoutMs) {
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    if (maxTemplateCacheSize > 0) {
        cacheBuilder = cacheBuilder.maximumSize(maxTemplateCacheSize);
    }//from ww  w.  j a va  2  s  .  co  m
    if (templateCacheTimeoutMs > 0) {
        cacheBuilder = cacheBuilder.expireAfterAccess(templateCacheTimeoutMs, TimeUnit.MILLISECONDS);
    }
    if (LOG.isTraceEnabled()) {
        cacheBuilder = cacheBuilder.removalListener(
                (notification) -> LOG.trace("Removing flow set template entry {} for cause: {} ",
                        notification.getKey(), notification.getCause()));
    }
    return cacheBuilder.build();
}

From source file:com.googlecode.webutilities.common.cache.impl.GoogleCache.java

public GoogleCache(CacheConfig<K, V> cacheConfig) {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().softValues();
    // if(reloadAfterAccess > 0)
    //     builder.expireAfterAccess(reloadAfterAccess, TimeUnit.SECONDS);
    if (cacheConfig.getReloadTime() > 0)
        builder.expireAfterWrite(cacheConfig.getReloadTime(), TimeUnit.SECONDS);
    googleCache = builder.build();
}

From source file:com.codeabovelab.dm.platform.cache.ConfigurableGuavaCacheManager.java

private com.google.common.cache.Cache<Object, SimpleValueWrapper> buildCache(CacheConfig cacheConfig) {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if (cacheConfig.getExpireAfterWrite() >= 0) {
        builder.expireAfterWrite(cacheConfig.getExpireAfterWrite(), TimeUnit.MILLISECONDS);
    }//from  w w  w .j  av a  2  s .  c  o  m
    return builder.build();
}

From source file:com.navercorp.pinpoint.profiler.modifier.spring.beans.interceptor.TargetBeanFilter.java

private Cache<Class<?>, Object> createCache() {
    final CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    builder.concurrencyLevel(CACHE_CONCURRENCY_LEVEL);
    builder.maximumSize(CACHE_SIZE);/*from www  . j  av a2 s. co  m*/
    builder.weakKeys();
    return builder.build();
}

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);/*w w w  . j  a  v  a 2  s.  com*/
    cacheBuilder.initialCapacity(maxActiveTraceSize);
    cacheBuilder.maximumSize(maxActiveTraceSize);
    // OOM defense
    cacheBuilder.weakValues();

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

From source file:com.google.api.control.aggregator.CheckAggregationOptions.java

/**
 * Creates a {@link Cache} configured by this instance.
 *
 * @param <T>/* ww  w  .java 2 s.  co m*/
 *            the type of the value stored in the Cache
 * @param out
 *            a concurrent {@code Deque} to which the cached values are
 *            added as they are removed from the cache
 * @param ticker
 *            the time source used to determine expiration
 * @return a {@link Cache} corresponding to this instance's values or
 *         {@code null} unless {@code #numEntries} is positive.
 */
@Nullable
public <T> Cache<String, T> createCache(final ConcurrentLinkedDeque<T> out, Ticker ticker) {
    Preconditions.checkNotNull(out, "The out deque cannot be null");
    Preconditions.checkNotNull(ticker, "The ticker cannot be null");
    if (numEntries <= 0) {
        return null;
    }
    final RemovalListener<String, T> listener = new RemovalListener<String, T>() {
        @Override
        public void onRemoval(RemovalNotification<String, T> notification) {
            out.addFirst(notification.getValue());
        }
    };
    CacheBuilder<String, T> b = CacheBuilder.newBuilder().maximumSize(numEntries).ticker(ticker)
            .removalListener(listener);
    if (expirationMillis >= 0) {
        b.expireAfterWrite(expirationMillis, TimeUnit.MILLISECONDS);
    }
    return b.build();
}

From source file:com.google.api.control.aggregator.ReportAggregationOptions.java

/**
 * Creates a {@link Cache} configured by this instance.
 *
 * @param <T>/*from   w  ww. j ava  2s. co m*/
 *            the type of the value stored in the Cache
 * @param out
 *            a concurrent {@code Deque} to which cached values are added as
 *            they are removed from the cache
 * @param ticker
 *            the time source used to determine expiration
 * @return a {@link Cache} corresponding to this instance's values or
 *         {@code null} unless {@code #numEntries} is positive.
 */
@Nullable
public <T> Cache<String, T> createCache(final ConcurrentLinkedDeque<T> out, Ticker ticker) {
    Preconditions.checkNotNull(out, "The out deque cannot be null");
    Preconditions.checkNotNull(ticker, "The ticker cannot be null");
    if (numEntries <= 0) {
        return null;
    }
    final RemovalListener<String, T> listener = new RemovalListener<String, T>() {
        @Override
        public void onRemoval(RemovalNotification<String, T> notification) {
            out.addFirst(notification.getValue());
        }
    };
    CacheBuilder<String, T> b = CacheBuilder.newBuilder().maximumSize(numEntries).ticker(ticker)
            .removalListener(listener);
    if (flushCacheEntryIntervalMillis >= 0) {
        b.expireAfterWrite(flushCacheEntryIntervalMillis, TimeUnit.MILLISECONDS);
    }
    return b.build();
}

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);
    }//from   w  ww  .j  a  va2s  .  co  m
    if (expire != null) {
        cacheBuilder.expireAfterAccess(expire.nanos(), TimeUnit.NANOSECONDS);
    }
    return cacheBuilder.build();
}

From source file:org.restheart.cache.impl.GuavaCache.java

public GuavaCache(long size, EXPIRE_POLICY expirePolicy, long ttl) {
    CacheBuilder builder = CacheBuilder.newBuilder();

    builder.maximumSize(size);/*  w  w  w  .  ja  v a2s.com*/

    if (ttl > 0 && expirePolicy == EXPIRE_POLICY.AFTER_WRITE) {
        builder.expireAfterWrite(ttl, TimeUnit.MILLISECONDS);
    } else if (ttl > 0 && expirePolicy == EXPIRE_POLICY.AFTER_READ) {
        builder.expireAfterAccess(ttl, TimeUnit.MILLISECONDS);
    }

    wrapped = builder.build();
}