@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; } }
@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)); }
@Override @Nullable public ValueWrapper get(Object key) { if (this.cache instanceof LoadingCache) { Object value = ((LoadingCache<Object, Object>) this.cache).get(key); return toValueWrapper(value); } return super.get(key); }
@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 @SuppressWarnings("NullAway") public V apply(@NonNull K key) { return cache.get(key); }
@Override public Optional<P> authenticate(C credentials) throws AuthenticationException { try (Timer.Context context = gets.time()) { return cache.get(credentials); } catch (CompletionException e) { final Throwable cause = e.getCause(); if (cause instanceof InvalidCredentialsException) { return Optional.empty(); } if (cause instanceof AuthenticationException) { throw (AuthenticationException) cause; } throw new AuthenticationException(cause); } }
@Override public boolean authorize(P principal, String role) { try (Timer.Context context = getsTimer.time()) { final ImmutablePair<P, String> cacheKey = ImmutablePair.of(principal, role); final Boolean result = cache.get(cacheKey); return result == null ? false : result; } catch (CompletionException e) { Throwable cause = e.getCause(); if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } if (cause instanceof Error) { throw (Error) cause; } throw e; } }
@Override public void render(View view, Locale locale, OutputStream output) throws IOException { final Configuration configuration = configurationCache.get(view.getClass()); if (configuration == null) { throw new ViewRenderException("Couldn't find view class " + view.getClass()); } try { final Charset charset = view.getCharset().orElseGet(() -> Charset.forName(configuration.getEncoding(locale))); final Template template = configuration.getTemplate(view.getTemplateName(), locale, charset.name()); template.process(view, new OutputStreamWriter(output, template.getEncoding())); } catch (Exception e) { throw new ViewRenderException(e); } }
@Override @SuppressWarnings({"PMD.PreserveStackTrace", "NullAway"}) public V get(K key) throws ExecutionException { requireNonNull(key); try { return cache.get(key); } catch (InvalidCacheLoadException e) { throw e; } catch (CacheLoaderException e) { throw new ExecutionException(e.getCause()); } catch (RuntimeException e) { throw new UncheckedExecutionException(e); } catch (Error e) { throw new ExecutionError(e); } }
@Override public void render(View view, Locale locale, OutputStream output) throws IOException { try { final MustacheFactory mustacheFactory = useCache ? factories.get(view.getClass()) : createNewMustacheFactory(view.getClass()); final Mustache template = mustacheFactory.compile(view.getTemplateName()); final Charset charset = view.getCharset().orElse(StandardCharsets.UTF_8); try (OutputStreamWriter writer = new OutputStreamWriter(output, charset)) { template.execute(writer, view); } } catch (Throwable e) { throw new ViewRenderException("Mustache template error: " + view.getTemplateName(), e); } }
@CacheSpec @CheckNoWriter @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) public void get_null(LoadingCache<Integer, Integer> cache, CacheContext context) { cache.get(null); }
@Override @SuppressWarnings({"PMD.PreserveStackTrace", "PMD.AvoidCatchingNPE", "NullAway"}) public V getUnchecked(K key) { try { return cache.get(key); } catch (NullPointerException | InvalidCacheLoadException e) { throw e; } catch (CacheLoaderException e) { throw new UncheckedExecutionException(e.getCause()); } catch (Exception e) { throw new UncheckedExecutionException(e); } catch (Error e) { throw new ExecutionError(e); } }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }) public void get_present(LoadingCache<Integer, Integer> cache, CacheContext context) { assertThat(cache.get(context.firstKey()), is(-context.firstKey())); assertThat(cache.get(context.middleKey()), is(-context.middleKey())); assertThat(cache.get(context.lastKey()), is(-context.lastKey())); assertThat(context, both(hasMissCount(0)).and(hasHitCount(3))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0))); }
@Test public void sanity() { LoadingCache<Integer, Integer> cache = Caffeine.newBuilder().build(k -> -k); assertEquals(-1, cache.get(1).intValue()); } }
@CacheSpec @CheckNoWriter @Test(dataProvider = "caches") public void get_absent_null(LoadingCache<Integer, Integer> cache, CacheContext context) { assertThat(cache.get(context.absentKey(), k -> null), is(nullValue())); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(loader = Loader.NULL) public void get_absent_null(LoadingCache<Integer, Integer> cache, CacheContext context) { assertThat(cache.get(context.absentKey()), is(nullValue())); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); }
@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)); }
@CacheSpec @CheckNoWriter @Test(dataProvider = "caches") public void get_absent(LoadingCache<Integer, Integer> cache, CacheContext context) { Integer key = context.absentKey(); Integer value = cache.get(key); assertThat(value, is(-key)); assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0))); }
@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 @CacheSpec(loader = Loader.EXCEPTIONAL) @Test(dataProvider = "caches", expectedExceptions = IllegalStateException.class) public void get_absent_failure(LoadingCache<Integer, Integer> cache, CacheContext context) { try { cache.get(context.absentKey()); } finally { assertThat(context, both(hasMissCount(1)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); } }