@Test public void maximumWeight_large() { Caffeine<?, ?> builder = Caffeine.newBuilder() .maximumWeight(Integer.MAX_VALUE).weigher(Weigher.singletonWeigher()); assertThat(builder.maximumWeight, is((long) Integer.MAX_VALUE)); assertThat(builder.weigher, is(Weigher.singletonWeigher())); Eviction<?, ?> eviction = builder.build().policy().eviction().get(); assertThat(eviction.getMaximum(), is((long) Integer.MAX_VALUE)); assertThat(eviction.isWeighted(), is(true)); }
@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 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)); }
private void resizeIfNecessary() { if (evictionPolicy.getMaximum() != size.get()) { evictionPolicy.setMaximum(size.get()); } }
private void logStats() { double maxMB = ((double) policy.getMaximum()) / ((double) (1024 * 1024)); double sizeMB = ((double) policy.weightedSize().getAsLong()) / ((double) (1024 * 1024)); double freeMB = maxMB - sizeMB; log.debug("Cache Size={}MB, Free={}MB, Max={}MB, Blocks={}", sizeMB, freeMB, maxMB, cache.estimatedSize()); log.debug(cache.stats().toString()); }
private Block load(Loader loader, Map<String,byte[]> resolvedDeps) { byte[] data = loader.load((int) Math.min(Integer.MAX_VALUE, policy.getMaximum()), resolvedDeps); if (data == null) { return null; } return new Block(data); }
private void resizeIfNecessary() { if (evictionPolicy.getMaximum() != size.get()) { evictionPolicy.setMaximum(size.get()); } }
@Override public long getMaxSize() { return policy.getMaximum(); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, removalListener = { Listener.DEFAULT, Listener.CONSUMING }) public void maximumSize_decrease(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { long newSize = context.maximumWeightOrSize() / 2; eviction.setMaximum(newSize); assertThat(eviction.getMaximum(), is(newSize)); if (context.initialSize() > newSize) { if (context.isZeroWeighted()) { assertThat(cache.estimatedSize(), is(context.initialSize())); assertThat(cache, hasRemovalNotifications(context, 0, RemovalCause.SIZE)); } else { assertThat(cache.estimatedSize(), is(newSize)); assertThat(cache, hasRemovalNotifications(context, newSize, RemovalCause.SIZE)); } } }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, weigher = { CacheWeigher.DEFAULT, CacheWeigher.TEN }, removalListener = { Listener.DEFAULT, Listener.CONSUMING }) public void maximumSize_decrease_min(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { eviction.setMaximum(0); assertThat(eviction.getMaximum(), is(0L)); if (context.initialSize() > 0) { long expect = context.isZeroWeighted() ? context.initialSize() : 0; assertThat(cache.estimatedSize(), is(expect)); } assertThat(cache, hasRemovalNotifications(context, context.initialSize(), RemovalCause.SIZE)); }
@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, maximumSize = Maximum.FULL, removalListener = Listener.REJECTING) public void maximumSize_increase_max(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { eviction.setMaximum(Long.MAX_VALUE); assertThat(cache.estimatedSize(), is(context.initialSize())); assertThat(eviction.getMaximum(), is(Long.MAX_VALUE - Integer.MAX_VALUE)); // impl detail }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void maximumSize_increase(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { eviction.setMaximum(2 * context.maximumWeightOrSize()); assertThat(cache.estimatedSize(), is(context.initialSize())); assertThat(eviction.getMaximum(), is(2 * context.maximumWeightOrSize())); }
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, removalListener = { Listener.DEFAULT, Listener.CONSUMING }) @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) public void maximumSize_decrease_negative(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { try { eviction.setMaximum(-1); } finally { assertThat(eviction.getMaximum(), is(context.maximumWeightOrSize())); assertThat(cache, hasRemovalNotifications(context, 0, RemovalCause.SIZE)); } }