com.github.benmanes.caffeine.cache.Weigher Java Examples
The following examples show how to use
com.github.benmanes.caffeine.cache.Weigher.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RepositoryCache.java From centraldogma with Apache License 2.0 | 6 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) public RepositoryCache(String cacheSpec, MeterRegistry meterRegistry) { this.cacheSpec = requireNonNull(validateCacheSpec(cacheSpec), "cacheSpec"); requireNonNull(meterRegistry, "meterRegistry"); final Caffeine<Object, Object> builder = Caffeine.from(cacheSpec); if (cacheSpec.contains("maximumWeight=")) { builder.weigher((Weigher<CacheableCall, Object>) CacheableCall::weigh); } cache = builder.recordStats() .buildAsync((key, executor) -> { logger.debug("Cache miss: {}", key); return key.execute(); }); CaffeineCacheMetrics.monitor(meterRegistry, cache, "repository"); }
Example #2
Source File: CacheFactory.java From caffeine with Apache License 2.0 | 5 votes |
/** Configures the maximum weight and returns if set. */ private boolean configureMaximumWeight() { if (config.getMaximumWeight().isPresent()) { caffeine.maximumWeight(config.getMaximumWeight().getAsLong()); Weigher<K, V> weigher = config.getWeigherFactory().map(Factory::create) .orElseThrow(() -> new IllegalStateException("Weigher not configured")); caffeine.weigher((K key, Expirable<V> expirable) -> { return weigher.weigh(key, expirable.get()); }); } return config.getMaximumWeight().isPresent(); }
Example #3
Source File: NashornScriptEngine.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
private void initScriptEngine() { LOG.debug("Initializing Nashorn script engine ..."); NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); scriptEngine = factory.getScriptEngine(s -> false); // create engine with class filter exposing no classes expressions = Caffeine.newBuilder() .maximumWeight(MAX_COMPILED_EXPRESSIONS_SCRIPTS_LENGTH) .weigher((Weigher<String, CompiledScript>) (key, value) -> key.length()) .build(((Compilable) this.scriptEngine)::compile); LOG.debug("Initialized Nashorn script engine"); }
Example #4
Source File: CaffeineTest.java From cache2k-benchmark with Apache License 2.0 | 5 votes |
@Test public void test() { Cache c = Caffeine.newBuilder().maximumWeight(1000).weigher(new Weigher<Object, Object>() { @Override public int weigh(final Object key, final Object value) { return value.hashCode() & 0x7f; } }).build(); c.put(1, 123); c.put(1, 512); c.put(1, 0); }
Example #5
Source File: TestingWeighers.java From caffeine with Apache License 2.0 | 4 votes |
/** * Returns a {@link Weigher} that returns the given {@code constant} for every request. */ static Weigher<Object, Object> constantWeigher(int constant) { return new ConstantWeigher(constant); }
Example #6
Source File: TestingWeighers.java From caffeine with Apache License 2.0 | 4 votes |
/** * Returns a {@link Weigher} that uses the integer key as the weight. */ static Weigher<Integer, Object> intKeyWeigher() { return new IntKeyWeigher(); }
Example #7
Source File: TestingWeighers.java From caffeine with Apache License 2.0 | 4 votes |
/** * Returns a {@link Weigher} that uses the integer value as the weight. */ static Weigher<Object, Integer> intValueWeigher() { return new IntValueWeigher(); }
Example #8
Source File: CacheEvictionTest.java From caffeine with Apache License 2.0 | 4 votes |
/** * With an unlimited-size cache with maxWeight of 0, entries weighing 0 should still be cached. * Entries with positive weight should not be cached (nor dump existing cache). */ public void testEviction_maxWeight_zero() { CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener(); IdentityLoader<Integer> loader = identityLoader(); // Even numbers are free, odd are too expensive Weigher<Integer, Integer> evensOnly = (k, v) -> k % 2; LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder() .maximumWeight(0) .weigher(evensOnly) .removalListener(removalListener) .executor(MoreExecutors.directExecutor()), loader); // 1 won't be cached assertThat(cache.getUnchecked(1)).isEqualTo(1); assertThat(cache.asMap().keySet()).isEmpty(); cache.cleanUp(); assertThat(removalListener.getCount()).isEqualTo(1); // 2 will be cached assertThat(cache.getUnchecked(2)).isEqualTo(2); assertThat(cache.asMap().keySet()).containsExactly(2); cache.cleanUp(); CacheTesting.checkValidState(cache); assertThat(removalListener.getCount()).isEqualTo(1); // 4 will be cached assertThat(cache.getUnchecked(4)).isEqualTo(4); assertThat(cache.asMap().keySet()).containsExactly(2, 4); cache.cleanUp(); assertThat(removalListener.getCount()).isEqualTo(1); // 5 won't be cached, won't dump cache assertThat(cache.getUnchecked(5)).isEqualTo(5); assertThat(cache.asMap().keySet()).containsExactly(2, 4); cache.cleanUp(); assertThat(removalListener.getCount()).isEqualTo(2); // Should we pepper more of these calls throughout the above? Where? CacheTesting.checkValidState(cache); }
Example #9
Source File: CaffeineConfiguration.java From caffeine with Apache License 2.0 | 2 votes |
/** * Returns the {@link Factory} for the {@link Weigher} to be used for the cache. * * @return the {@link Factory} for the {@link Weigher} */ public Optional<Factory<Weigher<K, V>>> getWeigherFactory() { return Optional.ofNullable(weigherFactory); }
Example #10
Source File: CaffeineConfiguration.java From caffeine with Apache License 2.0 | 2 votes |
/** * Set the {@link Factory} for the {@link Weigher}. * * @param factory the {@link Weigher} {@link Factory} */ @SuppressWarnings("unchecked") public void setWeigherFactory(Optional<Factory<? extends Weigher<K, V>>> factory) { weigherFactory = (Factory<Weigher<K, V>>) factory.orElse(null); }