@Override public boolean containsMatch(Set<String> matchExpressions, String metricName) { for (String regexExpression : matchExpressions) { final Pattern pattern = patternCache.get(regexExpression); if (pattern != null && pattern.matcher(metricName).matches()) { // just need to match on a single value - return as soon as we do return true; } } return false; } }
/** * Discards any cached principal for the collection of credentials satisfying the given predicate. * * @param predicate a predicate to filter credentials */ public void invalidateAll(Predicate<? super C> predicate) { final Set<C> keys = cache.asMap().keySet().stream() .filter(predicate) .collect(Collectors.toSet()); cache.invalidateAll(keys); }
/** * Discards any cached principal for the given credentials. * * @param credentials a set of credentials */ public void invalidate(C credentials) { cache.invalidate(credentials); }
public void run() throws InterruptedException { ConcurrentTestHarness.timeTasks(operation.maxThreads, () -> { int index = ThreadLocalRandom.current().nextInt(); for (;;) { Integer key = ints[index++ & MASK]; switch (operation) { case READ: cache.getIfPresent(key); break; case WRITE: cache.put(key, key); break; case REFRESH: cache.refresh(key); break; } } }); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, keys = ReferenceType.STRONG, writer = Writer.DISABLED) public void getAll_jdk8186171(CacheContext context) { class Key { @Override public int hashCode() { return 0; // to put keys in one bucket } } LoadingCache<Object, Integer> cache = context.build(key -> null); List<Key> keys = new ArrayList<>(); for (int i = 0; i < Population.FULL.size(); i++) { keys.add(new Key()); } Key key = Iterables.getLast(keys); Integer value = context.absentValue(); cache.put(key, value); Map<Object, Integer> result = cache.getAll(keys); assertThat(result.values(), not(hasItem(nullValue()))); assertThat(result, is(equalTo(ImmutableMap.of(key, value)))); }
V message = (V) messageGetStrategy.get(tuple); try { Map<String, Tuple> streamMessageMap = cache.get(key); if (streamMessageMap.containsKey(streamId)) { LOG.warn("Received key {} twice for stream {}", key, streamId); perfLog.log("emit-message", "key={}, elapsed time to emit messages", key); cache.invalidate(key); Tuple messageTuple = streamMessageMap.get("message:"); collector.ack(messageTuple); LOG.trace("Emitted message for key: {}", key); } else { cache.put(key, streamMessageMap); if(LOG.isDebugEnabled()) { LOG.debug("{}: Missed joining portions for {}. Expected {} != {}",
@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(LoadingCache<Integer, Integer> cache, CacheContext context) { Integer key = context.firstKey(); try { context.clear(); GcFinalization.awaitFullGc(); cache.get(key); } finally { context.disableRejectingCacheWriter(); assertThat(cache.asMap().isEmpty(), is(false)); } }
Expirable<V> expirable = cache.getIfPresent(key); if ((expirable != null) && !expirable.isEternal()) { millis = nanosToMillis((start == 0L) ? ticker.read() : start); if (expirable.hasExpired(millis)) { Expirable<V> expired = expirable; cache.asMap().computeIfPresent(key, (k, e) -> { if (e == expired) { dispatcher.publishExpired(this, key, expired.get()); expirable = cache.get(key); statistics.recordMisses(1L); } else {
@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)); }
cache.put(key, originalValue); assertThat(cache.get(key), is(originalValue)); assertThat(cache.getIfPresent(key), is(originalValue)); assertThat(cache.get(key), is(originalValue)); await().until(() -> cache.get(key), is(not(originalValue))); await().until(executor::getQueue, is(empty())); assertThat(reloading.get(), is(1)); assertThat(cache.get(key), is(1));
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void refresh_absent(LoadingCache<Integer, Integer> cache, CacheContext context) { cache.refresh(context.absentKey()); assertThat(cache.estimatedSize(), is(1 + context.initialSize())); assertThat(context, both(hasMissCount(0)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); // records a hit assertThat(cache.get(context.absentKey()), is(-context.absentKey())); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(refreshAfterWrite = Expire.ONE_MINUTE, population = { Population.PARTIAL, Population.FULL }) 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); assertThat(cache.getIfPresent(context.firstKey()), is(-context.firstKey())); assertThat(cache, hasRemovalNotifications(context, 1, RemovalCause.REPLACED)); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(refreshAfterWrite = Expire.ONE_MINUTE, population = { Population.PARTIAL, Population.FULL }) public void getFunc(LoadingCache<Integer, Integer> cache, CacheContext context) { Function<Integer, Integer> mappingFunction = context.original()::get; context.ticker().advance(30, TimeUnit.SECONDS); cache.get(context.firstKey(), mappingFunction); context.ticker().advance(45, TimeUnit.SECONDS); cache.get(context.lastKey(), mappingFunction); // refreshed assertThat(cache.estimatedSize(), is(context.initialSize())); assertThat(cache, hasRemovalNotifications(context, 1, RemovalCause.REPLACED)); }
/** * Discards any cached principal for the given collection of credentials. * * @param credentials a collection of credentials */ public void invalidateAll(Iterable<C> credentials) { cache.invalidateAll(credentials); }
public Map<String, ConcurrentMap> getCaches() { return maps.asMap(); }
@Test(dataProvider = "caches") @CacheSpec(population = Population.EMPTY, executor = CacheExecutor.THREADED, removalListener = Listener.CONSUMING) public void refresh_conflict(CacheContext context) { AtomicBoolean refresh = new AtomicBoolean(); Integer key = context.absentKey(); Integer original = 1; Integer updated = 2; Integer refreshed = 3; LoadingCache<Integer, Integer> cache = context.build(k -> { await().untilTrue(refresh); return refreshed; }); cache.put(key, original); cache.refresh(key); assertThat(cache.asMap().put(key, updated), is(original)); refresh.set(true); await().until(() -> context.consumedNotifications().size(), is(2)); List<Integer> removed = context.consumedNotifications().stream() .map(RemovalNotification::getValue).collect(toList()); assertThat(cache.getIfPresent(key), is(updated)); assertThat(removed, containsInAnyOrder(original, refreshed)); assertThat(cache, hasRemovalNotifications(context, 2, RemovalCause.REPLACED)); assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); }
@Test(dataProvider = "caches") @CacheSpec(population = Population.EMPTY, executor = CacheExecutor.THREADED, removalListener = Listener.CONSUMING) public void refresh_invalidate(CacheContext context) { AtomicBoolean refresh = new AtomicBoolean(); Integer key = context.absentKey(); Integer original = 1; Integer refreshed = 2; LoadingCache<Integer, Integer> cache = context.build(k -> { await().untilTrue(refresh); return refreshed; }); cache.put(key, original); cache.refresh(key); cache.invalidate(key); refresh.set(true); await().until(() -> cache.getIfPresent(key), is(refreshed)); await().until(() -> cache, hasRemovalNotifications(context, 1, RemovalCause.EXPLICIT)); await().until(() -> context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); }
@Test(dataProvider = "caches") @CacheSpec(population = Population.EMPTY, refreshAfterWrite = Expire.ONE_MINUTE, executor = CacheExecutor.THREADED, removalListener = Listener.CONSUMING) public void put(CacheContext context) { AtomicBoolean refresh = new AtomicBoolean(); Integer key = context.absentKey(); Integer original = 1; Integer updated = 2; Integer refreshed = 3; LoadingCache<Integer, Integer> cache = context.build(k -> { await().untilTrue(refresh); return refreshed; }); cache.put(key, original); context.ticker().advance(2, TimeUnit.MINUTES); assertThat(cache.getIfPresent(key), is(original)); assertThat(cache.asMap().put(key, updated), is(original)); refresh.set(true); await().until(() -> context.consumedNotifications().size(), is(2)); List<Integer> removed = context.consumedNotifications().stream() .map(RemovalNotification::getValue).collect(toList()); assertThat(cache.getIfPresent(key), is(updated)); assertThat(removed, containsInAnyOrder(original, refreshed)); assertThat(cache, hasRemovalNotifications(context, 2, RemovalCause.REPLACED)); assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); }
@Test(dataProvider = "caches") @CacheSpec(population = Population.EMPTY, refreshAfterWrite = Expire.ONE_MINUTE, executor = CacheExecutor.THREADED, removalListener = Listener.CONSUMING) public void invalidate(CacheContext context) { AtomicBoolean refresh = new AtomicBoolean(); Integer key = context.absentKey(); Integer original = 1; Integer refreshed = 2; LoadingCache<Integer, Integer> cache = context.build(k -> { await().untilTrue(refresh); return refreshed; }); cache.put(key, original); context.ticker().advance(2, TimeUnit.MINUTES); assertThat(cache.getIfPresent(key), is(original)); cache.invalidate(key); refresh.set(true); await().until(() -> cache.getIfPresent(key), is(refreshed)); await().until(() -> cache, hasRemovalNotifications(context, 1, RemovalCause.EXPLICIT)); await().until(() -> context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); }