Refine search
@Override public void record(long key) { Object value = cache.getIfPresent(key); if (value == null) { if (cache.estimatedSize() == maximumSize) { policyStats.recordEviction(); } cache.put(key, key); policyStats.recordMiss(); } else { policyStats.recordHit(); } }
@Override @Nullable public ValueWrapper putIfAbsent(Object key, @Nullable final Object value) { PutIfAbsentFunction callable = new PutIfAbsentFunction(value); Object result = this.cache.get(key, callable); return (callable.called ? null : toValueWrapper(result)); }
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)))); }
@Override public Task<Void> flush(Actor actor) { final RemoteReference actorReference = (RemoteReference) actor; masterCache.asMap().forEach((method, cache) -> { cache.invalidate(actorReference); }); return Task.done(); }
/** * Gets the human friendly location of where the violation was raised. */ public static String getMessage(ConstraintViolation<?> v, Invocable invocable) { final Pair<Path, ? extends ConstraintDescriptor<?>> of = Pair.of(v.getPropertyPath(), v.getConstraintDescriptor()); final String cachePrefix = PREFIX_CACHE.getIfPresent(of); if (cachePrefix == null) { final String prefix = calculatePrefix(v, invocable); PREFIX_CACHE.put(of, prefix); return prefix + v.getMessage(); } return cachePrefix + v.getMessage(); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, population = Population.EMPTY, keys = ReferenceType.STRONG, values = ReferenceType.STRONG) public void remove(Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) { cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1))); assertThat(cache.asMap().remove("a"), is(asList(1, 2, 3))); assertThat(cache.estimatedSize(), is(1L)); assertThat(eviction.weightedSize().getAsLong(), is(1L)); }
/** Performs the bulk load where the existing entries are retained. */ private void loadAllAndKeepExisting(Set<? extends K> keys) { List<K> keysToLoad = keys.stream() .filter(key -> !cache.asMap().containsKey(key)) .collect(toList()); Map<K, V> result = cacheLoader.get().loadAll(keysToLoad); for (Map.Entry<K, V> entry : result.entrySet()) { if ((entry.getKey() != null) && (entry.getValue() != null)) { putIfAbsentNoAwait(entry.getKey(), entry.getValue(), /* publishToWriter */ false); } } }
@Test(dataProvider = "caches") @CacheSpec(population = { Population.PARTIAL, Population.FULL }) public void invalidateAll_partial(Cache<Integer, Integer> cache, CacheContext context) { List<Integer> keys = cache.asMap().keySet().stream() .filter(i -> ((i % 2) == 0)) .collect(Collectors.toList()); cache.invalidateAll(keys); assertThat(cache.estimatedSize(), is(context.initialSize() - keys.size())); assertThat(cache, hasRemovalNotifications(context, keys.size(), RemovalCause.EXPLICIT)); verifyWriter(context, (verifier, writer) -> { verifier.deletedAll(Maps.filterKeys(context.original(), Predicates.in(keys)), RemovalCause.EXPLICIT); }); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, weigher = CacheWeigher.TEN) public void weightedSize(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { long weightedSize = 0; for (Integer key : cache.asMap().keySet()) { weightedSize += eviction.weightOf(key).getAsInt(); } assertThat(weightedSize, is(eviction.weightedSize().getAsLong())); assertThat(eviction.weightedSize().getAsLong(), is(10 * cache.estimatedSize())); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, population = Population.EMPTY, keys = ReferenceType.STRONG, values = ReferenceType.STRONG) public void replace_sameWeight(Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) { cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1))); cache.asMap().replace("a", asList(-1, -2, -3)); assertThat(cache.estimatedSize(), is(2L)); assertThat(eviction.weightedSize().getAsLong(), is(4L)); }
@Test(dataProvider = "caches", expectedExceptions = DeleteException.class) @CacheSpec(keys = ReferenceType.STRONG, values = {ReferenceType.WEAK, ReferenceType.SOFT}, implementation = Implementation.Caffeine, expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED, maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT, population = Population.FULL, stats = Stats.ENABLED, compute = Compute.SYNC, removalListener = Listener.CONSUMING, writer = Writer.EXCEPTIONAL) public void get_writerFails(Cache<Integer, Integer> cache, CacheContext context) { Integer key = context.firstKey(); try { context.clear(); GcFinalization.awaitFullGc(); cache.get(key, Function.identity()); } finally { context.disableRejectingCacheWriter(); assertThat(cache.asMap().isEmpty(), is(false)); } }
@Test(dataProvider = "caches", expectedExceptions = DeleteException.class) @CacheSpec(keys = ReferenceType.STRONG, values = {ReferenceType.WEAK, ReferenceType.SOFT}, implementation = Implementation.Caffeine, expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED, maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT, population = Population.FULL, stats = Stats.ENABLED, compute = Compute.SYNC, removalListener = Listener.CONSUMING, writer = Writer.EXCEPTIONAL) public void invalidate_writerFails(Cache<Integer, Integer> cache, CacheContext context) { Integer key = context.firstKey(); try { context.clear(); GcFinalization.awaitFullGc(); cache.invalidate(key); } finally { context.disableRejectingCacheWriter(); assertThat(cache.asMap().isEmpty(), is(false)); } }
@Override public void put(Object key, @Nullable Object value) { this.cache.put(key, toStoreValue(value)); }
@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 compute_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) { Integer key = context.absentKey(); cache.put(key, ImmutableList.of(1)); GcFinalization.awaitFullGc(); cache.asMap().compute(key, (k, v) -> ImmutableList.of(1, 2, 3)); assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L)); }
@Override @Nullable protected Object lookup(Object key) { return this.cache.getIfPresent(key); }
@CheckNoStats @Test(dataProvider = "caches", expectedExceptions = WriteException.class) @CacheSpec(implementation = Implementation.Caffeine, keys = ReferenceType.STRONG, compute = Compute.SYNC, writer = Writer.EXCEPTIONAL, removalListener = Listener.REJECTING) public void put_insert_writerFails(Cache<Integer, Integer> cache, CacheContext context) { try { cache.put(context.absentKey(), context.absentValue()); } finally { assertThat(cache.asMap(), equalTo(context.original())); } }
@Test(dataProvider = "caches") @CacheSpec(population = Population.EMPTY, expiryTime = Expire.ONE_MINUTE, mustExpireWithAnyOf = { AFTER_WRITE, VARIABLE }, expiry = { CacheExpiry.DISABLED, CacheExpiry.WRITE }, expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE}) public void get_writeTime(Cache<Integer, Integer> cache, CacheContext context) { Integer key = context.absentKey(); Integer value = context.absentValue(); cache.get(key, k -> { context.ticker().advance(5, TimeUnit.MINUTES); return value; }); assertThat(cache.estimatedSize(), is(1L)); assertThat(cache.getIfPresent(key), is(value)); }
@Test(dataProvider = "caches") @CacheSpec(values = {ReferenceType.WEAK, ReferenceType.SOFT}, population = Population.FULL) public void identity_values(Cache<Integer, Integer> cache, CacheContext context) { @SuppressWarnings("deprecation") Integer value = new Integer(context.original().get(context.firstKey())); assertThat(cache.asMap().containsValue(value), is(false)); }
@Test(dataProvider = "caches") @CacheSpec(requiresWeakOrSoft = true, expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED, maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT, population = Population.FULL, stats = Stats.ENABLED, removalListener = Listener.CONSUMING) public void invalidate(Cache<Integer, Integer> cache, CacheContext context) { Integer key = context.firstKey(); Integer value = cache.getIfPresent(key); context.clear(); GcFinalization.awaitFullGc(); awaitFullCleanup(cache); assertThat(cache.estimatedSize(), is(1L)); cache.invalidate(key); assertThat(value, is(notNullValue())); assertThat(cache.estimatedSize(), is(0L)); long count = context.initialSize() - 1; assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.COLLECTED)); verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.COLLECTED)); }