Java Code Examples for com.google.common.cache.CacheBuilder#expireAfterWrite()
The following examples show how to use
com.google.common.cache.CacheBuilder#expireAfterWrite() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GuavaCache.java From Mybatis-PageHelper with MIT License | 6 votes |
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 { 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(); }
Example 2
Source File: TenantCacheImpl.java From phoenix with Apache License 2.0 | 6 votes |
private Cache<ImmutableBytesPtr, CacheEntry> buildCache(final int ttl, final boolean isPersistent) { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (isPersistent) { builder.expireAfterWrite(ttl, TimeUnit.MILLISECONDS); } else { builder.expireAfterAccess(ttl, TimeUnit.MILLISECONDS); } return builder .ticker(getTicker()) .removalListener(new RemovalListener<ImmutableBytesPtr, CacheEntry>(){ @Override public void onRemoval(RemovalNotification<ImmutableBytesPtr, CacheEntry> notification) { if (isPersistent || !notification.getValue().getUsePersistentCache()) { Closeables.closeAllQuietly(Collections.singletonList(notification.getValue())); } } }) .build(); }
Example 3
Source File: CommonUtil.java From ChangeSkin with MIT License | 6 votes |
public static <K, V> ConcurrentMap<K, V> buildCache(int seconds, int maxSize) { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (seconds > 0) { builder.expireAfterWrite(seconds, TimeUnit.SECONDS); } if (maxSize > 0) { builder.maximumSize(maxSize); } return builder.build(new CacheLoader<K, V>() { @Override public V load(K key) { throw new UnsupportedOperationException("Not supported yet."); } }).asMap(); }
Example 4
Source File: CachingTableProvider.java From samza with Apache License 2.0 | 6 votes |
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); } 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; }
Example 5
Source File: GuavaCacheFactoryBean.java From spring-cache-demo with GNU General Public License v2.0 | 6 votes |
@Override public void afterPropertiesSet() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (maximumSize != null) { builder.maximumSize(maximumSize); } if (expireAfterAccessInSeconds != null) { builder.expireAfterAccess(expireAfterAccessInSeconds, TimeUnit.SECONDS); } if (expireAfterWriteInSeconds != null) { builder.expireAfterWrite(expireAfterWriteInSeconds, TimeUnit.SECONDS); } com.google.common.cache.Cache<Object, Object> guavaCache= builder.build(); this.cache = new GuavaCache(this.name, guavaCache, this.allowNullValues); }
Example 6
Source File: StandAloneLimiter.java From neural with MIT License | 6 votes |
@Override protected boolean tryRefresh(LimiterConfig config) { // rate limiter this.rateLimiter = AdjustableRateLimiter.create(config.getRate().getMaxRate()); // concurrent limiter this.semaphore = new AdjustableSemaphore(config.getConcurrent().getMaxPermit(), true); // counter limiter // request limiter CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); cacheBuilder.expireAfterWrite(config.getCounter().getTimeout(), TimeUnit.MILLISECONDS); this.counter = cacheBuilder.build(CacheLoader.from(() -> new AtomicLong(0))); // the refresh rateLimiter rateLimiter.setRate(config.getRate().getRateUnit()); // the refresh semaphore semaphore.setMaxPermits(config.getConcurrent().getMaxPermit()); return true; }
Example 7
Source File: HotKeyListener.java From EVCache with Apache License 2.0 | 5 votes |
private Cache<String, Integer> getCache(String appName) { Property<Boolean> throttleFlag = throttleMap.get(appName); if(throttleFlag == null) { throttleFlag = EVCacheConfig.getInstance().getPropertyRepository().get("EVCacheThrottler." + appName + ".throttle.hot.keys", Boolean.class).orElse(false); throttleMap.put(appName, throttleFlag); } if(log.isDebugEnabled()) log.debug("Throttle hot keys : " + throttleFlag); if(!throttleFlag.get()) { return null; } Cache<String, Integer> cache = cacheMap.get(appName); if(cache != null) return cache; final Property<Integer> _cacheDuration = EVCacheConfig.getInstance().getPropertyRepository().get("EVCacheThrottler." + appName + ".inmemory.expire.after.write.duration.ms", Integer.class).orElse(10000); final Property<Integer> _exireAfterAccessDuration = EVCacheConfig.getInstance().getPropertyRepository().get("EVCacheThrottler." + appName + ".inmemory.expire.after.access.duration.ms", Integer.class).orElse(10000); final Property<Integer> _cacheSize = EVCacheConfig.getInstance().getPropertyRepository().get("EVCacheThrottler." + appName + ".inmemory.cache.size", Integer.class).orElse(100); CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().recordStats(); if(_cacheSize.get() > 0) { builder = builder.maximumSize(_cacheSize.get()); } if(_exireAfterAccessDuration.get() > 0) { builder = builder.expireAfterAccess(_exireAfterAccessDuration.get(), TimeUnit.MILLISECONDS); } else if(_cacheDuration.get() > 0) { builder = builder.expireAfterWrite(_cacheDuration.get(), TimeUnit.MILLISECONDS); } cache = builder.build(); cacheMap.put(appName, cache); return cache; }
Example 8
Source File: GuavaCacheFactory.java From cache2k-benchmark with Apache License 2.0 | 5 votes |
private CacheBuilder builder(final int _maxElements) { CacheBuilder cb = CacheBuilder.newBuilder() .maximumSize(_maxElements); if (withExpiry) { cb.expireAfterWrite(2 * 60, TimeUnit.SECONDS); } return cb; }
Example 9
Source File: CacheStore.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
@Override public void connect() throws IOException { open = true; if (numInitCacheLines > maxCacheSize) { logger.warn("numInitCacheLines = {} is greater than maxCacheSize = {}, maxCacheSize was set to {}", numInitCacheLines, maxCacheSize, numInitCacheLines); maxCacheSize = numInitCacheLines; } 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(); if (entryExpiryStrategy == ExpiryType.NO_EVICTION) { return; } this.cleanupScheduler = Executors.newScheduledThreadPool(1); cleanupScheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { cache.cleanUp(); } }, cacheCleanupIntervalInMillis, cacheCleanupIntervalInMillis, TimeUnit.MILLISECONDS); }
Example 10
Source File: ConfigurableGuavaCacheManager.java From haven-platform with Apache License 2.0 | 5 votes |
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); } return builder.build(); }
Example 11
Source File: StandAloneLimiter.java From neural with MIT License | 5 votes |
@Override public synchronized boolean refresh(LimiterConfig limiterConfig) throws Exception { super.refresh(limiterConfig); // rate limiter rateLimiter = AdjustableRateLimiter.create(limiterConfig.getMaxPermitRate()); // request limiter CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); cacheBuilder.expireAfterWrite(limiterConfig.getRequestInterval().toMillis(), TimeUnit.MILLISECONDS); cache = cacheBuilder.build(); // concurrent limiter semaphore = new AdjustableSemaphore(limiterConfig.getMaxPermitConcurrent(), true); try { if (0 < limiterConfig.getMaxPermitConcurrent()) { // the refresh semaphore semaphore.setMaxPermits(limiterConfig.getMaxPermitConcurrent()); } if (0 < limiterConfig.getRatePermit()) { // the refresh rateLimiter rateLimiter.setRate(limiterConfig.getRatePermit()); } return true; } catch (Exception e) { log.error("The refresh local limiter is exception", e); } return false; }
Example 12
Source File: DefaultSimpleCache.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * Construct a cache using the specified capacity and name. * * @param maxItems The cache capacity. 0 = use {@link #DEFAULT_CAPACITY} * @param useMaxItems Whether the maxItems value should be applied as a size-cap for the cache. * @param cacheName An arbitrary cache name. */ @SuppressWarnings("unchecked") public DefaultSimpleCache(int maxItems, boolean useMaxItems, int ttlSecs, int maxIdleSecs, String cacheName) { if (maxItems == 0) { maxItems = DEFAULT_CAPACITY; } else if (maxItems < 0) { throw new IllegalArgumentException("maxItems may not be negative, but was " + maxItems); } this.maxItems = maxItems; this.useMaxItems = useMaxItems; this.ttlSecs = ttlSecs; this.maxIdleSecs = maxIdleSecs; setBeanName(cacheName); // The map will have a bounded size determined by the maxItems member variable. @SuppressWarnings("rawtypes") CacheBuilder builder = CacheBuilder.newBuilder(); if (useMaxItems) { builder.maximumSize(maxItems); } if (ttlSecs > 0) { builder.expireAfterWrite(ttlSecs, TimeUnit.SECONDS); } if (maxIdleSecs > 0) { builder.expireAfterAccess(maxIdleSecs, TimeUnit.SECONDS); } builder.concurrencyLevel(32); cache = (Cache<K, AbstractMap.SimpleImmutableEntry<K, V>>) builder.build(); }
Example 13
Source File: GuavaCacheImpl.java From RxCache with Apache License 2.0 | 5 votes |
public GuavaCacheImpl(long maxSize, CacheConfig cacheConfig) { super(maxSize); CacheBuilder cacheBuilder = CacheBuilder .newBuilder() .recordStats() .maximumSize(maxSize); if (cacheConfig!=null) { if (cacheConfig.expireDuration>0 && cacheConfig.expireTimeUnit!=null) { cacheBuilder.expireAfterWrite(cacheConfig.expireDuration,cacheConfig.expireTimeUnit); } if (cacheConfig.refreshDuration>0 && cacheConfig.refreshTimeUnit!=null) { cacheBuilder.refreshAfterWrite(cacheConfig.refreshDuration,cacheConfig.refreshTimeUnit); } } cache = cacheBuilder.build(new CacheLoader<String, Object>(){ @Override public String load(String key) throws Exception { return key; } }); }
Example 14
Source File: GuavaConcurrentMapFactory.java From icure-backend with GNU General Public License v2.0 | 5 votes |
@Override public <K, V> ConcurrentMap<K, V> getMap(final ConcurrentMapListener<K, V> listener) { // Create cache builder CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); // Set expireAfterWrite if (expireDuration != null && expireUnit != null) { cacheBuilder = cacheBuilder.expireAfterWrite(expireDuration, expireUnit); } // Configure listener if (listener != null) { cacheBuilder.removalListener((RemovalListener<K, V>) notification -> { K key = notification.getKey(); V value = notification.getValue(); switch (notification.getCause()) { case REPLACED: listener.entryUpdated(key, value); break; case EXPLICIT: listener.entryRemoved(key, value); break; case COLLECTED: case EXPIRED: case SIZE: listener.entryEvicted(key, value); break; } }); } // Build cache Cache<K, V> cache = cacheBuilder.build(); return cache.asMap(); }
Example 15
Source File: GuavaCacheFactory.java From joyrpc with Apache License 2.0 | 5 votes |
@Override public <K, V> Cache<K, V> build(final String name, final CacheConfig<K, V> config) { CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); if (config.getExpireAfterWrite() > 0) { cacheBuilder.expireAfterWrite(config.getExpireAfterWrite(), TimeUnit.MILLISECONDS); } cacheBuilder.maximumSize(config.getCapacity() > 0 ? config.getCapacity() : Long.MAX_VALUE); com.google.common.cache.Cache<K, CacheObject<V>> cache = cacheBuilder.build(); return new GuavaCache<>(cache, config); }
Example 16
Source File: CachingHiveMetastore.java From presto with Apache License 2.0 | 5 votes |
private static CacheBuilder<Object, Object> newCacheBuilder(OptionalLong expiresAfterWriteMillis, OptionalLong refreshMillis, long maximumSize) { CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); if (expiresAfterWriteMillis.isPresent()) { cacheBuilder = cacheBuilder.expireAfterWrite(expiresAfterWriteMillis.getAsLong(), MILLISECONDS); } if (refreshMillis.isPresent() && (expiresAfterWriteMillis.isEmpty() || expiresAfterWriteMillis.getAsLong() > refreshMillis.getAsLong())) { cacheBuilder = cacheBuilder.refreshAfterWrite(refreshMillis.getAsLong(), MILLISECONDS); } cacheBuilder = cacheBuilder.maximumSize(maximumSize); return cacheBuilder; }
Example 17
Source File: GuavaCacheTest.java From semantic-metrics with Apache License 2.0 | 4 votes |
@Override public <K, V> CacheBuilder<K, V> setup(CacheBuilder<K, V> builder) { return builder.expireAfterWrite(6, TimeUnit.HOURS); }
Example 18
Source File: GuavaUtils.java From t-io with Apache License 2.0 | 4 votes |
/** * * @param concurrencyLevel * @param timeToLiveSeconds 设置写缓存后过期时间(单位:秒) * @param timeToIdleSeconds 设置读缓存后过期时间(单位:秒) * @param initialCapacity * @param maximumSize * @param recordStats * @param removalListener * @return */ public static <K, V> LoadingCache<K, V> createLoadingCache(Integer concurrencyLevel, Long timeToLiveSeconds, Long timeToIdleSeconds, Integer initialCapacity, Integer maximumSize, boolean recordStats, RemovalListener<K, V> removalListener) { if (removalListener == null) { removalListener = new RemovalListener<K, V>() { @Override public void onRemoval(RemovalNotification<K, V> notification) { log.info(notification.getKey() + " was removed"); } }; } CacheBuilder<K, V> cacheBuilder = CacheBuilder.newBuilder().removalListener(removalListener); //设置并发级别为8,并发级别是指可以同时写缓存的线程数 cacheBuilder.concurrencyLevel(concurrencyLevel); if (timeToLiveSeconds != null && timeToLiveSeconds > 0) { //设置写缓存后8秒钟过期 cacheBuilder.expireAfterWrite(timeToLiveSeconds, TimeUnit.SECONDS); } if (timeToIdleSeconds != null && timeToIdleSeconds > 0) { //设置访问缓存后8秒钟过期 cacheBuilder.expireAfterAccess(timeToIdleSeconds, TimeUnit.SECONDS); } //设置缓存容器的初始容量为10 cacheBuilder.initialCapacity(initialCapacity); //设置缓存最大容量为100,超过100之后就会按照LRU最近最少使用算法来移除缓存项 cacheBuilder.maximumSize(maximumSize); if (recordStats) { //设置要统计缓存的命中率 cacheBuilder.recordStats(); } //build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存 LoadingCache<K, V> loadingCache = cacheBuilder.build(new CacheLoader<K, V>() { @Override public V load(K key) throws Exception { return null; } }); return loadingCache; // for (int i = 0; i < 20; i++) // { // //从缓存中得到数据,由于我们没有设置过缓存,所以需要通过CacheLoader加载缓存数据 // Long student = studentCache.get("p"); // System.out.println(student); // //休眠1秒 // TimeUnit.SECONDS.sleep(1); // } // System.out.println("cache stats:"); //最后打印缓存的命中率等 情况 // System.out.println(studentCache.stats().toString()); }
Example 19
Source File: DeDupProcessor.java From datacollector with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings("unchecked") protected List<ConfigIssue> init() { List<ConfigIssue> issues = super.init(); if (recordCountWindow <= 0) { issues.add(getContext().createConfigIssue(Groups.DE_DUP.name(), "recordCountWindow", Errors.DEDUP_00, recordCountWindow)); } if (timeWindowSecs < 0) { issues.add(getContext().createConfigIssue(Groups.DE_DUP.name(), "timeWindowSecs", Errors.DEDUP_01, timeWindowSecs)); } if (compareFields == SelectFields.SPECIFIED_FIELDS && fieldsToCompare.isEmpty()) { issues.add(getContext().createConfigIssue(Groups.DE_DUP.name(), "compareFields", Errors.DEDUP_02)); } if (issues.isEmpty()) { hasher = HashingUtil.getHasher(HashingUtil.HashType.MURMUR3_128); funnel = (compareFields == SelectFields.ALL_FIELDS) ? HashingUtil.getRecordFunnel( Collections.EMPTY_LIST, false, true, '\u0000' ) : HashingUtil.getRecordFunnel(fieldsToCompare, false, true, '\u0000'); Map<String, Object> runnerSharedMap = getContext().getStageRunnerSharedMap(); synchronized (runnerSharedMap) { if(!runnerSharedMap.containsKey(CACHE_KEY)) { CacheBuilder cacheBuilder = CacheBuilder.newBuilder(); if (timeWindowSecs > 0) { cacheBuilder.expireAfterWrite(timeWindowSecs, TimeUnit.SECONDS); } if(LOG.isDebugEnabled()) { cacheBuilder.recordStats(); } hashCache = cacheBuilder.build(); runnerSharedMap.put(CACHE_KEY, hashCache); } else { hashCache = (Cache<HashCode, HashCode>) runnerSharedMap.get(CACHE_KEY); } } cacheCleaner = new CacheCleaner(hashCache, "DeDupProcessor", 10 * 60 * 1000); hashBuffer = XEvictingQueue.create(recordCountWindow); hashAttrName = getInfo() + ".hash"; uniqueLane = getContext().getOutputLanes().get(OutputStreams.UNIQUE.ordinal()); duplicateLane = getContext().getOutputLanes().get(OutputStreams.DUPLICATE.ordinal()); } return issues; }
Example 20
Source File: IrisSettings.java From arcusplatform with Apache License 2.0 | 4 votes |
public static CacheBuilder<Object,Object> configurableCacheBuilder(String base, CacheBuilder<Object,Object> builder) { // If there is a full cache specification then that overrides everything String spec = IrisSettings.getStringProperty(base + ".spec", ""); if (!spec.isEmpty()) { return CacheBuilder.from(spec); } CacheBuilder<Object,Object> bld = builder; int concurrency = IrisSettings.getIntegerProperty(base + ".concurrency", -1); if (concurrency > 0) { bld = bld.concurrencyLevel(concurrency); } long write = IrisSettings.getLongProperty(base + ".expire.write", -1L); if (write > 0) { bld = bld.expireAfterWrite(write, TimeUnit.MILLISECONDS); } long access = IrisSettings.getLongProperty(base + ".expire.access", -1L); if (access > 0) { bld = bld.expireAfterAccess(access, TimeUnit.MILLISECONDS); } long refresh = IrisSettings.getLongProperty(base + ".refresh.write", -1L); if (refresh > 0) { bld = bld.refreshAfterWrite(refresh, TimeUnit.MILLISECONDS); } int initsz = IrisSettings.getIntegerProperty(base + ".initial.capacity", -1); if (initsz > 0) { bld = bld.initialCapacity(initsz); } int maxsz = IrisSettings.getIntegerProperty(base + ".max.size", -1); if (maxsz > 0) { bld = bld.maximumSize(maxsz); } boolean soft = IrisSettings.getBooleanProperty(base + ".soft.values", false); if (soft) { bld = bld.softValues(); } return bld; }