@SuppressWarnings("FutureReturnValueIgnored") public Stresser() { ThreadFactory threadFactory = new ThreadFactoryBuilder() .setPriority(Thread.MAX_PRIORITY) .setDaemon(true) .build(); Executors.newSingleThreadScheduledExecutor(threadFactory) .scheduleAtFixedRate(this::status, STATUS_INTERVAL, STATUS_INTERVAL, SECONDS); cache = Caffeine.newBuilder() .maximumSize(operation.maxEntries) .recordStats() .build(key -> key); local = (BoundedLocalCache<Integer, Integer>) cache.asMap(); ints = new Integer[TOTAL_KEYS]; Arrays.setAll(ints, key -> { cache.put(key, key); return key; }); cache.cleanUp(); stopwatch = Stopwatch.createStarted(); status(); }
@Test(dataProvider = "caches") @CacheSpec(mustExpireWithAnyOf = { AFTER_ACCESS, VARIABLE }, expiryTime = Expire.ONE_MINUTE, expiry = { CacheExpiry.DISABLED, CacheExpiry.ACCESS }, expireAfterAccess = Expire.ONE_MINUTE, loader = Loader.IDENTITY, population = { Population.PARTIAL, Population.FULL }) public void get(LoadingCache<Integer, Integer> cache, CacheContext context) { context.ticker().advance(30, TimeUnit.SECONDS); cache.get(context.firstKey()); context.ticker().advance(45, TimeUnit.SECONDS); assertThat(cache.get(context.lastKey()), is(context.lastKey())); cache.cleanUp(); assertThat(cache.estimatedSize(), is(2L)); context.ticker().advance(45, TimeUnit.SECONDS); cache.cleanUp(); assertThat(cache.estimatedSize(), is(1L)); long count = context.initialSize(); assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPIRED)); verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.EXPIRED)); }
@Test(dataProvider = "caches") @CacheSpec(population = Population.EMPTY, removalListener = Listener.CONSUMING, mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE }, expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS }, expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE}, expiryTime = Expire.ONE_MINUTE, expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE}) @SuppressWarnings("FutureReturnValueIgnored") public void get_async(AsyncLoadingCache<Integer, Integer> cache, CacheContext context) { CompletableFuture<Integer> future = new CompletableFuture<Integer>(); cache.get(context.absentKey(), (k, e) -> future); context.ticker().advance(2, TimeUnit.MINUTES); cache.synchronous().cleanUp(); assertThat(cache, hasRemovalNotifications(context, 0, RemovalCause.EXPIRED)); future.complete(context.absentValue()); context.ticker().advance(30, TimeUnit.SECONDS); assertThat(cache.getIfPresent(context.absentKey()), is(future)); context.ticker().advance(1, TimeUnit.MINUTES); assertThat(cache.getIfPresent(context.absentKey()), is(nullValue())); cache.synchronous().cleanUp(); assertThat(cache, hasRemovalNotifications(context, 1, RemovalCause.EXPIRED)); verifyWriter(context, (verifier, writer) -> verifier.deletions(1, RemovalCause.EXPIRED)); }
@Test(dataProvider = "caches") @CacheSpec(population = Population.EMPTY, removalListener = Listener.CONSUMING, mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE }, expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS }, expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE}, expiryTime = Expire.ONE_MINUTE, expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE}) @SuppressWarnings("FutureReturnValueIgnored") public void put_insert_async(AsyncLoadingCache<Integer, Integer> cache, CacheContext context) { CompletableFuture<Integer> future = new CompletableFuture<Integer>(); cache.put(context.absentKey(), future); context.ticker().advance(2, TimeUnit.MINUTES); cache.synchronous().cleanUp(); assertThat(cache, hasRemovalNotifications(context, 0, RemovalCause.EXPIRED)); future.complete(context.absentValue()); context.ticker().advance(30, TimeUnit.SECONDS); assertThat(cache.getIfPresent(context.absentKey()), is(future)); context.ticker().advance(1, TimeUnit.MINUTES); assertThat(cache.getIfPresent(context.absentKey()), is(nullValue())); cache.synchronous().cleanUp(); assertThat(cache, hasRemovalNotifications(context, 1, RemovalCause.EXPIRED)); verifyWriter(context, (verifier, writer) -> verifier.deletions(1, RemovalCause.EXPIRED)); }
@Test(dataProvider = "caches") @CacheSpec(population = { Population.PARTIAL, Population.FULL }, mustExpireWithAnyOf = { AFTER_WRITE, VARIABLE }, expireAfterWrite = Expire.ONE_MINUTE, expiry = { CacheExpiry.DISABLED, CacheExpiry.WRITE }, expiryTime = Expire.ONE_MINUTE, loader = {Loader.IDENTITY, Loader.BULK_IDENTITY}) public void getAll(LoadingCache<Integer, Integer> cache, CacheContext context) { context.ticker().advance(30, TimeUnit.SECONDS); assertThat(cache.getAll(ImmutableList.of(context.firstKey(), context.absentKey())), is(ImmutableMap.of(context.firstKey(), -context.firstKey(), context.absentKey(), context.absentKey()))); context.ticker().advance(45, TimeUnit.SECONDS); cache.cleanUp(); assertThat(cache.getAll(ImmutableList.of(context.firstKey(), context.absentKey())), is(ImmutableMap.of(context.firstKey(), context.firstKey(), context.absentKey(), context.absentKey()))); assertThat(cache.estimatedSize(), is(2L)); long count = context.initialSize(); assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPIRED)); verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.EXPIRED)); }
@Test(dataProvider = "caches") @CacheSpec(population = { Population.PARTIAL, Population.FULL }, mustExpireWithAnyOf = { AFTER_WRITE, VARIABLE }, expireAfterWrite = Expire.ONE_MINUTE, expiry = { CacheExpiry.DISABLED, CacheExpiry.WRITE }, expiryTime = Expire.ONE_MINUTE) public void get(LoadingCache<Integer, Integer> cache, CacheContext context) { context.ticker().advance(30, TimeUnit.SECONDS); cache.get(context.firstKey()); cache.get(context.absentKey()); context.ticker().advance(45, TimeUnit.SECONDS); cache.cleanUp(); assertThat(cache.estimatedSize(), is(1L)); assertThat(cache.getIfPresent(context.absentKey()), is(-context.absentKey())); long count = context.initialSize(); assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPIRED)); verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.EXPIRED)); }
@Override public void prune() { cache.cleanUp(); }
@Test(dataProvider = "caches") @CacheSpec(mustExpireWithAnyOf = { AFTER_ACCESS, VARIABLE }, expiry = { CacheExpiry.DISABLED, CacheExpiry.ACCESS }, expiryTime = Expire.ONE_MINUTE, expireAfterAccess = Expire.ONE_MINUTE, loader = {Loader.IDENTITY, Loader.BULK_IDENTITY}, population = { Population.PARTIAL, Population.FULL }) public void getAll(LoadingCache<Integer, Integer> cache, CacheContext context) { context.ticker().advance(30, TimeUnit.SECONDS); assertThat(cache.getAll(ImmutableList.of(context.firstKey(), context.middleKey())), is(ImmutableMap.of(context.firstKey(), -context.firstKey(), context.middleKey(), -context.middleKey()))); context.ticker().advance(45, TimeUnit.SECONDS); cache.cleanUp(); assertThat(cache.getAll(ImmutableList.of(context.firstKey(), context.absentKey())), is(ImmutableMap.of(context.firstKey(), -context.firstKey(), context.absentKey(), context.absentKey()))); context.ticker().advance(45, TimeUnit.SECONDS); cache.cleanUp(); assertThat(cache.getAll(ImmutableList.of(context.middleKey(), context.absentKey())), is(ImmutableMap.of(context.middleKey(), context.middleKey(), context.absentKey(), context.absentKey()))); assertThat(cache.estimatedSize(), is(3L)); long count = context.initialSize() - 1; assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPIRED)); verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.EXPIRED)); }
@Override public void cleanInternal() { LOGGER.debug("Beginning to clean up the cache storage to remove expiring tokens"); this.storage.cleanUp(); LOGGER.debug("Estimated total of [{}] token(s) remain in the cache and may be removed in future iterations", this.storage.estimatedSize()); }
@InvokeOnHeader(CaffeineConstants.ACTION_CLEANUP) public void onCleanUp(Message message) throws Exception { cache.cleanUp(); setResult(message, true, null, null); }
@PreDestroy public void destroy() throws InterruptedException { if (visitors == null) return; try { try { if (visitorExpirer != null) { if (visitorExpirerFuture != null) { visitorExpirerFuture.cancel(false); visitorExpirerFuture = null; } visitorExpirer.shutdown(); visitorExpirer.awaitTermination(15L, TimeUnit.SECONDS); visitorExpirer = null; } } finally { repository.saveAll(cachedVisitorStream().filter(Visitor::isDirty).collect(toList())); expire(); } } finally { visitors.cleanUp(); visitors = null; } }