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

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

Introduction

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

Prototype

public CacheBuilder<K, V> expireAfterAccess(long duration, TimeUnit unit) 

Source Link

Document

Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, the most recent replacement of its value, or its last access.

Usage

From source file:org.rapidoid.plugins.cache.guava.GuavaCache.java

@SuppressWarnings("unchecked")
private static <K, V> CacheBuilder<K, V> builder(long timeToLiveMs, boolean resetTimeToLiveWhenAccessed) {
    CacheBuilder<K, V> builder = (CacheBuilder<K, V>) CacheBuilder.newBuilder();

    if (resetTimeToLiveWhenAccessed) {
        return builder.expireAfterAccess(timeToLiveMs, TimeUnit.MILLISECONDS);
    } else {//from   w w  w  .  ja  v a 2s . com
        return builder.expireAfterWrite(timeToLiveMs, TimeUnit.MILLISECONDS);
    }
}

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

public static CacheConfig expireAfterAccess(final Duration expiryTime) {
    return new CacheConfig() {
        @Override/*from www  . j  a v  a2s.c o m*/
        public CacheBuilder<Object, Object> configure(CacheBuilder<Object, Object> builder) {
            LOG.info("Expires values {} ms after last access", expiryTime.getMillis());
            return builder.expireAfterAccess(expiryTime.getMillis(), TimeUnit.MILLISECONDS);
        }
    };
}

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);//from   www.java  2s . c o  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.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 w  w  w  .  j  ava  2s  . 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:com.google.gitiles.ConfigUtil.java

/**
 * Get a {@link CacheBuilder} from a config.
 *
 * @param config JGit config object./*from   w  w w  . j av  a  2s  .co m*/
 * @param name name of the cache subsection under the "cache" section.
 * @return a new cache builder.
 */
public static CacheBuilder<Object, Object> getCacheBuilder(Config config, String name) {
    CacheBuilder<Object, Object> b = CacheBuilder.newBuilder();
    try {
        if (config.getString("cache", name, "maximumWeight") != null) {
            b.maximumWeight(config.getLong("cache", name, "maximumWeight", 20 << 20));
        }
        if (config.getString("cache", name, "maximumSize") != null) {
            b.maximumSize(config.getLong("cache", name, "maximumSize", 16384));
        }
        Duration expireAfterWrite = getDuration(config, "cache", name, "expireAfterWrite", null);
        if (expireAfterWrite != null) {
            b.expireAfterWrite(expireAfterWrite.getMillis(), TimeUnit.MILLISECONDS);
        }
        Duration expireAfterAccess = getDuration(config, "cache", name, "expireAfterAccess", null);
        if (expireAfterAccess != null) {
            b.expireAfterAccess(expireAfterAccess.getMillis(), TimeUnit.MILLISECONDS);
        }
        // Add other methods as needed.
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Error getting CacheBuilder for " + name, e);
    } catch (IllegalStateException e) {
        throw new IllegalStateException("Error getting CacheBuilder for " + name, e);
    }
    return b;
}

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  w  w w.ja  va  2  s . c  om*/
    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:net.maritimecloud.mms.server.rest.TailLogger.java

public TailLogger() {
    CacheBuilder<Object, Object> b = CacheBuilder.newBuilder();
    b.expireAfterAccess(1, TimeUnit.HOURS);
    cache = b.build();
}

From source file:com.p000ison.dev.simpleclans2.claiming.data.ChunkCache.java

public ChunkCache(int initial, int maxSize, long duration) {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();

    builder.maximumSize(maxSize);/*from w  w w.j a  v a2s  .c o m*/
    builder.expireAfterAccess(duration, TimeUnit.SECONDS);
    builder.initialCapacity(initial);
    //        cache = builder.build(this);
}

From source file:org.elasticsearch.index.cache.query.parser.resident.ResidentQueryParserCache.java

@Inject
public ResidentQueryParserCache(Index index, @IndexSettings Settings indexSettings) {
    super(index, indexSettings);

    this.maxSize = componentSettings.getAsInt("max_size", 100);
    this.expire = componentSettings.getAsTime("expire", null);
    logger.debug("using [resident] query cache with max_size [{}], expire [{}]", maxSize, expire);

    CacheBuilder cacheBuilder = CacheBuilder.newBuilder().maximumSize(maxSize);
    if (expire != null) {
        cacheBuilder.expireAfterAccess(expire.nanos(), TimeUnit.NANOSECONDS);
    }/*w w w . j  a  va 2  s  .  c  o m*/

    this.cache = cacheBuilder.build();
}

From source file:kr.debop4j.core.cache.HashMapCacheRepository.java

public HashMapCacheRepository(long validFor) {
    if (validFor > 0)
        setExpiry(validFor);/* w  w  w  . ja v  a2  s  .  c o  m*/

    CacheBuilder builder = CacheBuilder.newBuilder();

    if (validFor > 0)
        builder.expireAfterAccess(validFor, TimeUnit.MINUTES);

    cache = builder.build();
}