List of usage examples for com.google.common.cache CacheBuilder expireAfterAccess
public CacheBuilder<K, V> expireAfterAccess(long duration, TimeUnit unit)
From source file:org.apache.samza.table.caching.CachingTableProvider.java
private ReadWriteTable createDefaultCacheTable(String tableId, JavaTableConfig tableConfig) { long readTtlMs = Long.parseLong(tableConfig.getForTable(tableId, CachingTableDescriptor.READ_TTL_MS, "-1")); long writeTtlMs = Long .parseLong(tableConfig.getForTable(tableId, CachingTableDescriptor.WRITE_TTL_MS, "-1")); long cacheSize = Long.parseLong(tableConfig.getForTable(tableId, CachingTableDescriptor.CACHE_SIZE, "-1")); CacheBuilder cacheBuilder = CacheBuilder.newBuilder(); if (readTtlMs != -1) { cacheBuilder.expireAfterAccess(readTtlMs, TimeUnit.MILLISECONDS); }//from ww w . ja v a2 s. c om if (writeTtlMs != -1) { cacheBuilder.expireAfterWrite(writeTtlMs, TimeUnit.MILLISECONDS); } if (cacheSize != -1) { cacheBuilder.maximumSize(cacheSize); } logger.info(String.format("Creating default cache with: readTtl=%d, writeTtl=%d, maxSize=%d", readTtlMs, writeTtlMs, cacheSize)); GuavaCacheTable cacheTable = new GuavaCacheTable(tableId + "-def-cache", cacheBuilder.build()); cacheTable.init(this.context); return cacheTable; }
From source file:org.apache.samza.table.caching.descriptors.CachingTableProvider.java
private ReadWriteTable createDefaultCacheTable(String tableId) { long readTtlMs = Long.parseLong(tableSpec.getConfig().getOrDefault(READ_TTL_MS, "-1")); long writeTtlMs = Long.parseLong(tableSpec.getConfig().getOrDefault(WRITE_TTL_MS, "-1")); long cacheSize = Long.parseLong(tableSpec.getConfig().getOrDefault(CACHE_SIZE, "-1")); CacheBuilder cacheBuilder = CacheBuilder.newBuilder(); if (readTtlMs != -1) { cacheBuilder.expireAfterAccess(readTtlMs, TimeUnit.MILLISECONDS); }/* w ww. j a v a 2 s .co m*/ if (writeTtlMs != -1) { cacheBuilder.expireAfterWrite(writeTtlMs, TimeUnit.MILLISECONDS); } if (cacheSize != -1) { cacheBuilder.maximumSize(cacheSize); } logger.info(String.format("Creating default cache with: readTtl=%d, writeTtl=%d, maxSize=%d", readTtlMs, writeTtlMs, cacheSize)); GuavaCacheTable cacheTable = new GuavaCacheTable(tableId + "-def-cache", cacheBuilder.build()); cacheTable.init(this.context); return cacheTable; }
From source file:org.commonjava.aprox.folo.data.FoloRecordCache.java
@PostConstruct public void buildCache() { final CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); builder.expireAfterAccess(config.getCacheTimeoutSeconds(), TimeUnit.SECONDS).removalListener(this); recordCache = builder.build(this); }
From source file:kr.debop4j.core.cache.ConcurrentHashMapCacheRepository.java
/** * Instantiates a new Concurrent hash map cache repository. * * @param validFor the valid for//from ww w. java2 s .co m */ public ConcurrentHashMapCacheRepository(long validFor) { if (validFor > 0) setExpiry(validFor); CacheBuilder builder = CacheBuilder.newBuilder().concurrencyLevel(4); if (validFor > 0) builder.expireAfterAccess(validFor, TimeUnit.MINUTES); cache = builder.build(); }
From source file:com.datatorrent.lib.database.CacheStore.java
public CacheStore(CacheProperties properties) { Preconditions.checkNotNull(properties.entryExpiryStrategy, "expiryType"); CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); if (properties.entryExpiryStrategy == ExpiryType.EXPIRE_AFTER_ACCESS) { cacheBuilder.expireAfterAccess(properties.entryExpiryDurationInMillis, TimeUnit.MILLISECONDS); } else if (properties.entryExpiryStrategy == ExpiryType.EXPIRE_AFTER_WRITE) { cacheBuilder.expireAfterWrite(properties.entryExpiryDurationInMillis, TimeUnit.MILLISECONDS); }/* w ww.jav a 2 s .c o m*/ cache = cacheBuilder.build(); this.cleanupScheduler = Executors.newScheduledThreadPool(1); cleanupScheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { cache.cleanUp(); } }, properties.cacheCleanupIntervalInMillis, properties.cacheCleanupIntervalInMillis, TimeUnit.MILLISECONDS); }
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 . j av a2s. 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:org.hypoport.springGuavaCacheAdapter.SpringGuavaCacheAdapter.java
private void createGuavaCache() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (maximumSize != null) { builder.maximumSize(maximumSize); }/*from w w w. j av a2 s .c om*/ if (expireAfterAccessInSeconds != null) { builder.expireAfterAccess(expireAfterAccessInSeconds, SECONDS); } if (expireAfterWriteInSeconds != null) { builder.expireAfterWrite(expireAfterWriteInSeconds, SECONDS); } cache = builder.build(); }
From source file:com.hazelcast.client.GuavaNearCacheImpl.java
public GuavaNearCacheImpl(NearCacheConfig nc, final MapClientProxy<K, V> map) { this.map = map; CacheBuilder cacheBuilder = CacheBuilder.newBuilder().maximumSize(nc.getMaxSize()); if (nc.getTimeToLiveSeconds() > 0) cacheBuilder.expireAfterWrite(nc.getTimeToLiveSeconds(), TimeUnit.SECONDS); if (nc.getMaxIdleSeconds() > 0) cacheBuilder.expireAfterAccess(nc.getMaxIdleSeconds(), TimeUnit.SECONDS); cache = cacheBuilder.build(new CacheLoader() { @Override/*from w ww .j a v a2s . co m*/ public Object load(Object o) throws Exception { try { return map.get0(o); } catch (Exception e) { throw new ExecutionException(e); } } }); }
From source file:org.locationtech.geogig.di.caching.CacheFactory.java
protected synchronized void createCache() { if (cache != null) { return;/* w w w. ja v a 2s. c om*/ } if (!cacheIsEnabled()) { this.cache = NO_CACHE; return; } final int maxSize = getConfig("maxSize", 50_000); final int concurrencyLevel = getConfig("concurrencyLevel", 4); final int expireSeconds = getConfig("expireSeconds", 300); final int initialCapacity = getConfig("initialCapacity", 10 * 1000); CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); cacheBuilder = cacheBuilder.maximumSize(maxSize); cacheBuilder.expireAfterAccess(expireSeconds, TimeUnit.SECONDS); cacheBuilder.initialCapacity(initialCapacity); cacheBuilder.concurrencyLevel(concurrencyLevel); cacheBuilder.softValues(); try { this.cache = cacheBuilder.build(); } catch (RuntimeException e) { LOGGER.error( "Error configuring cache '{}' with maxSize: {}, expireSeconds: {}, initialCapacity: {}, concurrencyLevel: {}", configKeywordPrefix, maxSize, expireSeconds, initialCapacity, concurrencyLevel, e); throw e; } LOGGER.debug( "Cache '{}' configured with maxSize: {}, expireSeconds: {}, initialCapacity: {}, concurrencyLevel: {}", configKeywordPrefix, maxSize, expireSeconds, initialCapacity, concurrencyLevel); }
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 www .ja v a2s. co m*/ if (expire != null) { cacheBuilder.expireAfterAccess(expire.nanos(), TimeUnit.NANOSECONDS); } return cacheBuilder.build(); }