@Test(expectedExceptions = IllegalStateException.class) public void ticker_twice() { Caffeine.newBuilder().ticker(Ticker.systemTicker()).ticker(Ticker.systemTicker()); }
EntryIterator(BoundedLocalCache<K, V> cache) { this.iterator = cache.data.values().iterator(); this.now = cache.expirationTicker().read(); this.cache = cache; }
@NonNull Ticker getTicker() { boolean useTicker = expiresVariable() || expiresAfterAccess() || expiresAfterWrite() || refreshes() || isRecordingStats(); return useTicker ? (ticker == null) ? Ticker.systemTicker() : ticker : Ticker.disabledTicker(); }
@Override public Ticker statsTicker() { return Ticker.disabledTicker(); }
@Override public Ticker expirationTicker() { return Ticker.disabledTicker(); }
/** @return the current time in milliseconds */ protected final long currentTimeMillis() { return nanosToMillis(ticker.read()); }
private Cache<String, Task> getAddressableCache(final Cache<Addressable, Cache<String, Task>> methodCache, final Addressable addressable) { return methodCache.get(addressable, key -> { Caffeine<Object, Object> builder = Caffeine.newBuilder(); if (cacheExecutor != null) { builder.executor(cacheExecutor); } return builder .ticker(clock == null ? Ticker.systemTicker() : () -> TimeUnit.MILLISECONDS.toNanos(clock.millis())) .build(); }); }
@Nonnull Ticker getTicker() { boolean useTicker = expiresVariable() || expiresAfterAccess() || expiresAfterWrite() || refreshes() || isRecordingStats(); return useTicker ? (ticker == null) ? Ticker.systemTicker() : ticker : Ticker.disabledTicker(); }
@Override public Ticker expirationTicker() { return Ticker.disabledTicker(); }
@Override public boolean containsValue(Object value) { requireNonNull(value); long now = expirationTicker().read(); for (Node<K, V> node : data.values()) { if (node.containsValue(value) && !hasExpired(node, now) && (node.getKey() != null)) { return true; } } return false; }
public void testTicker_setTwice() { Ticker testTicker = Ticker.systemTicker(); Caffeine<Object, Object> builder = Caffeine.newBuilder().ticker(testTicker); try { // even to the same instance is not allowed builder.ticker(testTicker); fail(); } catch (IllegalStateException expected) {} }
@Override public Ticker statsTicker() { return Ticker.disabledTicker(); }
@Override public Map<K, V> getAll(Set<? extends K> keys) { requireNotClosed(); boolean statsEnabled = statistics.isEnabled(); long now = statsEnabled ? ticker.read() : 0L; Map<K, Expirable<V>> result = getAndFilterExpiredEntries(keys, true); if (statsEnabled) { statistics.recordGetTime(ticker.read() - now); } return copyMap(result); }
/** * Retrieves the cache for a CacheResponse-annotated method. * One is created if necessary. */ private Cache<Addressable, Cache<String, Task>> getMethodCache(Method method) { final CacheResponse cacheResponse = cacheResponseCache.getAnnotation(method); if (cacheResponse == null) { throw new IllegalArgumentException("Passed non-CacheResponse method."); } // if cached, return; otherwise create, cache and return return masterCache.get(method, key -> { Caffeine<Object, Object> builder = Caffeine.newBuilder(); if (cacheExecutor != null) { builder.executor(cacheExecutor); } return builder .ticker(clock == null ? Ticker.systemTicker() : () -> TimeUnit.MILLISECONDS.toNanos(clock.millis())) .maximumSize(cacheResponse.maxEntries()) .expireAfterWrite(cacheResponse.ttlDuration(), cacheResponse.ttlUnit()) .build(); }); }
@Override public Ticker expirationTicker() { return Ticker.disabledTicker(); }
@Override public void removeAll(Set<? extends K> keys) { requireNotClosed(); keys.forEach(Objects::requireNonNull); boolean statsEnabled = statistics.isEnabled(); long start = statsEnabled ? ticker.read() : 0L; Set<K> keysToRemove = new HashSet<>(keys); CacheWriterException e = deleteAllToCacheWriter(keysToRemove); long removed = keysToRemove.stream() .map(this::removeNoCopyOrAwait) .filter(Objects::nonNull) .count(); dispatcher.awaitSynchronous(); if (statsEnabled) { statistics.recordRemovals(removed); statistics.recordRemoveTime(ticker.read() - start); } if (e != null) { throw e; } }
/** * Create a new message distributor. * * @param periodDurationMillis The period duration in milliseconds. * @param profileTimeToLiveMillis The time-to-live of a profile in milliseconds. * @param maxNumberOfRoutes The max number of unique routes to maintain. After this is exceeded, lesser * used routes will be evicted from the internal cache. */ public DefaultMessageDistributor( long periodDurationMillis, long profileTimeToLiveMillis, long maxNumberOfRoutes) { this(periodDurationMillis, profileTimeToLiveMillis, maxNumberOfRoutes, Ticker.systemTicker()); }
@Override public Ticker expirationTicker() { return Ticker.disabledTicker(); }
@Override public Map<K, Expirable<V>> loadAll(Iterable<? extends K> keys) { try { boolean statsEnabled = statistics.isEnabled(); long start = statsEnabled ? ticker.read() : 0L; Map<K, Expirable<V>> result = delegate.loadAll(keys).entrySet().stream() .filter(entry -> (entry.getKey() != null) && (entry.getValue() != null)) .collect(Collectors.toMap(Map.Entry::getKey, entry -> new Expirable<>(entry.getValue(), expireTimeMS()))); for (Map.Entry<K, Expirable<V>> entry : result.entrySet()) { dispatcher.publishCreated(cache, entry.getKey(), entry.getValue().get()); } if (statsEnabled) { // Subtracts the load time from the get time statistics.recordGetTime(start - ticker.read()); } return result; } catch (CacheLoaderException e) { throw e; } catch (RuntimeException e) { throw new CacheLoaderException(e); } }
public static <T> CachingPartitionAllocator<T> createDefault(int numModuli) { // TODO (jkong): This should ideally be configurable. DistributingModulusGenerator modulusGenerator = new DistributingModulusGenerator(numModuli); return new CachingPartitionAllocator<>( modulusGenerator, ForkJoinPool.commonPool(), Ticker.systemTicker(), Duration.of(5, ChronoUnit.MINUTES)); }