private void checkContainsInOrder(Cache<Integer, Integer> cache, Integer... expect) { cache.cleanUp(); List<Integer> evictionList = ImmutableList.copyOf( cache.policy().eviction().get().coldest(Integer.MAX_VALUE).keySet()); assertThat(cache.asMap().size(), is(equalTo(expect.length))); assertThat(cache.asMap().keySet(), containsInAnyOrder(expect)); assertThat(evictionList, is(equalTo(asList(expect)))); }
params[i] = cache.asMap(); } else if (clazz.isAssignableFrom(Policy.Eviction.class)) { params[i] = cache.policy().eviction().get(); } else if (clazz.isAssignableFrom(Policy.Expiration.class)) { params[i] = expirationPolicy(parameters[i], cache); } else if (clazz.isAssignableFrom(Policy.VarExpiration.class)) { params[i] = cache.policy().expireVariably().get();
public static void setup(MeterRegistry registry, MeterIdPrefix idPrefix, Cache<?, ?> cache, Ticker ticker) { requireNonNull(cache, "cache"); if (!cache.policy().isRecordingStats()) { return; } final CaffeineMetrics metrics = MicrometerUtil.register( registry, idPrefix, CaffeineMetrics.class, CaffeineMetrics::new); metrics.add(cache, ticker); }
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT, expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED, refreshAfterWrite = Expire.DISABLED, keys = ReferenceType.STRONG, values = ReferenceType.STRONG) @Test(dataProvider = "caches") public void noPolicy(Cache<Integer, Integer> cache, CacheContext context) { assertThat(cache.policy().eviction(), is(Optional.empty())); assertThat(cache.policy().expireAfterWrite(), is(Optional.empty())); assertThat(cache.policy().expireAfterAccess(), is(Optional.empty())); assertThat(cache.policy().refreshAfterWrite(), is(Optional.empty())); }
/** Returns the fixed expiration policy for the given parameter. */ private static Policy.Expiration<Integer, Integer> expirationPolicy( Parameter parameter, Cache<Integer, Integer> cache) { if (parameter.isAnnotationPresent(ExpireAfterAccess.class)) { return cache.policy().expireAfterAccess().get(); } else if (parameter.isAnnotationPresent(ExpireAfterWrite.class)) { return cache.policy().expireAfterWrite().get(); } else if (parameter.isAnnotationPresent(RefreshAfterWrite.class)) { return cache.policy().refreshAfterWrite().get(); } throw new AssertionError("Expiration parameter must have a qualifier annotation"); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, expiry = CacheExpiry.DISABLED) public void expireVariably_notEnabled(Cache<Integer, Integer> cache) { assertThat(cache.policy().expireVariably(), is(Optional.empty())); }
@Test public void expireAfterAccess_small() { Caffeine<?, ?> builder = Caffeine.newBuilder().expireAfterAccess(0, TimeUnit.MILLISECONDS); assertThat(builder.expireAfterAccessNanos, is(0L)); Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); }
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT, expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED, refreshAfterWrite = Expire.DISABLED, keys = ReferenceType.STRONG, values = ReferenceType.STRONG) @Test(dataProvider = "caches") public void noPolicy_async(AsyncLoadingCache<Integer, Integer> cache, CacheContext context) { assertThat(cache.synchronous().policy().eviction(), is(Optional.empty())); assertThat(cache.synchronous().policy().expireAfterWrite(), is(Optional.empty())); assertThat(cache.synchronous().policy().expireAfterAccess(), is(Optional.empty())); assertThat(cache.synchronous().policy().refreshAfterWrite(), is(Optional.empty())); } }
@Override public <T> void put(String appid, String id, T object, Long ttlSeconds) { if (ttlSeconds == null || ttlSeconds <= 0L) { put(appid, id, object); return; } if (!StringUtils.isBlank(id) && object != null && !StringUtils.isBlank(appid)) { String key = key(appid, id); cache.policy().expireVariably().ifPresent((t) -> { t.put(key, object, ttlSeconds, TimeUnit.SECONDS); }); logger.debug("Cache.put() {} {} ttl {}", appid, id, ttlSeconds); } }
@Test public void expireAfterAccess_duration_small() { Caffeine<?, ?> builder = Caffeine.newBuilder().expireAfterAccess(Duration.ofMillis(0)); assertThat(builder.expireAfterAccessNanos, is(0L)); Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); }
@Test public void maximumSize_small() { Caffeine<?, ?> builder = Caffeine.newBuilder().maximumSize(0); assertThat(builder.maximumSize, is(0L)); Cache<?, ?> cache = builder.build(); assertThat(cache.policy().eviction().get().getMaximum(), is(0L)); }
@Override public <T> void put(String appid, String id, T object, Long ttlSeconds) { if (ttlSeconds == null || ttlSeconds <= 0L) { put(appid, id, object); return; } if (!StringUtils.isBlank(id) && object != null && !StringUtils.isBlank(appid)) { String key = key(appid, id); cache.policy().expireVariably().ifPresent((t) -> { t.put(key, object, ttlSeconds, TimeUnit.SECONDS); }); logger.debug("Cache.put() {} {} ttl {}", appid, id, ttlSeconds); } }
@CacheSpec @CheckNoWriter @CheckNoStats @Test(dataProvider = "caches") public void stats(Cache<Integer, Integer> cache, CacheContext context) { CacheStats stats = cache.stats() .plus(new CacheStats(1, 2, 3, 4, 5, 6, 7) .minus(new CacheStats(6, 5, 4, 3, 2, 1, 0))); assertThat(stats, is(new CacheStats(0, 0, 0, 1, 3, 5, 7))); assertThat(cache.policy().isRecordingStats(), is(context.isRecordingStats())); }
@Test public void expireAfterAccess_large() { Caffeine<?, ?> builder = Caffeine.newBuilder() .expireAfterAccess(Integer.MAX_VALUE, TimeUnit.NANOSECONDS); assertThat(builder.expireAfterAccessNanos, is((long) Integer.MAX_VALUE)); Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); }
@Test public void maximumSize_large() { Caffeine<?, ?> builder = Caffeine.newBuilder().maximumSize(Integer.MAX_VALUE); assertThat(builder.maximumSize, is((long) Integer.MAX_VALUE)); Cache<?, ?> cache = builder.build(); assertThat(cache.policy().eviction().get().getMaximum(), is((long) Integer.MAX_VALUE)); }
@Test public void expireAfterAccess_duration_large() { Caffeine<?, ?> builder = Caffeine.newBuilder() .expireAfterAccess(Duration.ofNanos(Integer.MAX_VALUE)); assertThat(builder.expireAfterAccessNanos, is((long) Integer.MAX_VALUE)); Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); }
@Test public void maximumWeight_small() { Caffeine<?, ?> builder = Caffeine.newBuilder() .maximumWeight(0).weigher(Weigher.singletonWeigher()); assertThat(builder.weigher, is(Weigher.singletonWeigher())); assertThat(builder.maximumWeight, is(0L)); Eviction<?, ?> eviction = builder.build().policy().eviction().get(); assertThat(eviction.getMaximum(), is(0L)); assertThat(eviction.isWeighted(), is(true)); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, expiryTime = Expire.ONE_MINUTE, mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE }, expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS }, expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE}, expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE}) public void putIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) { cache.put(1, ImmutableList.of(1)); context.ticker().advance(1, TimeUnit.MINUTES); cache.asMap().putIfAbsent(1, ImmutableList.of(1, 2, 3)); assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L)); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, requiresWeakOrSoft = true, expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED, maximumSize = Maximum.UNREACHABLE, weigher = CacheWeigher.COLLECTION, population = Population.EMPTY, stats = Stats.ENABLED, removalListener = Listener.DEFAULT, writer = Writer.DISABLED) public void put_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) { Integer key = context.absentKey(); cache.put(key, ImmutableList.of(1)); GcFinalization.awaitFullGc(); assertThat(cache.asMap().put(key, ImmutableList.of(1, 2, 3)), context.isStrongValues() ? is(ImmutableList.of(1)) : nullValue()); assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L)); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, expiryTime = Expire.ONE_MINUTE, mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE }, expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS }, expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE}, expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE}) public void computeIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) { cache.put(1, ImmutableList.of(1)); context.ticker().advance(1, TimeUnit.MINUTES); cache.asMap().computeIfAbsent(1, k -> ImmutableList.of(1, 2, 3)); assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L)); }