com.github.benmanes.caffeine.cache.LoadingCache Java Examples
The following examples show how to use
com.github.benmanes.caffeine.cache.LoadingCache.
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: CacheTest.java From zuihou-admin-boot with Apache License 2.0 | 6 votes |
@Test public void test() { LoadingCache<String, Map<String, Object>> caches = Caffeine.newBuilder() .maximumSize(10) .refreshAfterWrite(10000, TimeUnit.MINUTES) .build(new CacheLoader<String, Map<String, Object>>() { @Nullable @Override public Map<String, Object> load(@NonNull String s) throws Exception { Map<String, Object> map = new HashMap<>(); map.put("aaa", "aaa"); return map; } } ); Map<String, Object> aaa = caches.get("aaa"); System.out.println(aaa); Map<String, Object> bbb = caches.get("aaa"); System.out.println(bbb); Map<String, Object> bbb1 = caches.get("aaa1"); System.out.println(bbb1); }
Example #2
Source File: CacheTest.java From zuihou-admin-cloud with Apache License 2.0 | 6 votes |
@Test public void test() { LoadingCache<String, Map<String, Object>> caches = Caffeine.newBuilder() .maximumSize(10) .refreshAfterWrite(10000, TimeUnit.MINUTES) .build(new CacheLoader<String, Map<String, Object>>() { @Nullable @Override public Map<String, Object> load(@NonNull String s) throws Exception { Map<String, Object> map = new HashMap<>(); map.put("aaa", "aaa"); return map; } } ); Map<String, Object> aaa = caches.get("aaa"); System.out.println(aaa); Map<String, Object> bbb = caches.get("aaa"); System.out.println(bbb); Map<String, Object> bbb1 = caches.get("aaa1"); System.out.println(bbb1); }
Example #3
Source File: CaffeineCache.java From t-io with Apache License 2.0 | 6 votes |
public static CaffeineCache register(String cacheName, Long timeToLiveSeconds, Long timeToIdleSeconds, RemovalListener<String, Serializable> removalListener) { CaffeineCache caffeineCache = map.get(cacheName); if (caffeineCache == null) { synchronized (CaffeineCache.class) { caffeineCache = map.get(cacheName); if (caffeineCache == null) { Integer initialCapacity = 10; Integer maximumSize = 5000000; boolean recordStats = false; LoadingCache<String, Serializable> loadingCache = CaffeineUtils.createLoadingCache(cacheName, timeToLiveSeconds, timeToIdleSeconds, initialCapacity, maximumSize, recordStats, removalListener); Integer temporaryMaximumSize = 500000; LoadingCache<String, Serializable> temporaryLoadingCache = CaffeineUtils.createLoadingCache(cacheName, 10L, (Long) null, initialCapacity, temporaryMaximumSize, recordStats, removalListener); caffeineCache = new CaffeineCache(cacheName, loadingCache, temporaryLoadingCache); caffeineCache.setTimeToIdleSeconds(timeToIdleSeconds); caffeineCache.setTimeToLiveSeconds(timeToLiveSeconds); map.put(cacheName, caffeineCache); } } } return caffeineCache; }
Example #4
Source File: MetricsStatsCounterTest.java From caffeine with Apache License 2.0 | 6 votes |
@Test public void metrics() { // Use a registry that is exported using a Reporter (via console, JMX, Graphite, etc) MetricRegistry registry = new MetricRegistry(); // Create the cache with a dedicated, uniquely named stats counter LoadingCache<Integer, Integer> cache = Caffeine.newBuilder() .recordStats(() -> new MetricsStatsCounter(registry, "example")) .build(key -> key); // Perform application work for (int i = 0; i < 4; i++) { cache.get(1); } // Statistics can be queried and reported on assertThat(cache.stats().hitCount(), is(3L)); assertThat(registry.counter("example.hits").getCount(), is(3L)); }
Example #5
Source File: GuildSettingsUtils.java From SkyBot with GNU Affero General Public License v3.0 | 6 votes |
private static void loadGuildSettings(DatabaseAdapter databaseAdapter, LoadingCache<Long, GuildSettings> guildSettings) { logger.info("Loading Guild settings."); databaseAdapter.getGuildSettings( (storedSettings) -> { storedSettings.forEach( (setting) -> guildSettings.put(setting.getGuildId(), setting) ); logger.info("Loaded settings for " + guildSettings.estimatedSize() + " guilds."); return null; } ); }
Example #6
Source File: CaffeineMetricSupport.java From armeria with Apache License 2.0 | 6 votes |
void add(Cache<?, ?> cache, Ticker ticker) { synchronized (cacheRefs) { for (CacheReference ref : cacheRefs) { if (ref.get() == cache) { // Do not aggregate more than once for the same instance. return; } } cacheRefs.add(new CacheReference(cache, ticker)); } if (cache instanceof LoadingCache && hasLoadingCache.compareAndSet(false, true)) { // Add the following meters only for LoadingCache and only once. final String loads = idPrefix.name("loads"); parent.more().counter(loads, idPrefix.tags("result", "success"), this, func(LOAD_SUCCESS_COUNT, ref -> ref.cacheStats.loadSuccessCount())); parent.more().counter(loads, idPrefix.tags("result", "failure"), this, func(LOAD_FAILURE_COUNT, ref -> ref.cacheStats.loadFailureCount())); parent.more().counter(idPrefix.name(Flags.useLegacyMeterNames() ? "loadDuration" : "load.duration"), idPrefix.tags(), this, func(TOTAL_LOAD_TIME, ref -> ref.cacheStats.totalLoadTime())); } }
Example #7
Source File: CacheMetricsCollectorTest.java From client_java with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Test public void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception { CacheLoader<String, String> loader = mock(CacheLoader.class); when(loader.load(anyString())) .thenReturn("First User") .thenThrow(new RuntimeException("Seconds time fails")) .thenReturn("Third User"); LoadingCache<String, String> cache = Caffeine.newBuilder().recordStats().build(loader); CollectorRegistry registry = new CollectorRegistry(); CacheMetricsCollector collector = new CacheMetricsCollector().register(registry); collector.addCache("loadingusers", cache); cache.get("user1"); cache.get("user1"); try { cache.get("user2"); } catch (Exception e) { // ignoring. } cache.get("user3"); assertMetric(registry, "caffeine_cache_hit_total", "loadingusers", 1.0); assertMetric(registry, "caffeine_cache_miss_total", "loadingusers", 3.0); assertMetric(registry, "caffeine_cache_load_failure_total", "loadingusers", 1.0); assertMetric(registry, "caffeine_cache_loads_total", "loadingusers", 3.0); assertMetric(registry, "caffeine_cache_load_duration_seconds_count", "loadingusers", 3.0); assertMetricGreatThan(registry, "caffeine_cache_load_duration_seconds_sum", "loadingusers", 0.0); }
Example #8
Source File: CaffeineCache.java From spring-analysis-note with MIT License | 5 votes |
@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); }
Example #9
Source File: CacheProvider.java From caffeine with Apache License 2.0 | 5 votes |
/** Returns a new cache generator. */ private static CacheGenerator newCacheGenerator(Method testMethod) { CacheSpec cacheSpec = testMethod.getAnnotation(CacheSpec.class); requireNonNull(cacheSpec, "@CacheSpec not found"); Options options = Options.fromSystemProperties(); // Inspect the test parameters for interface constraints (loading, async) boolean isAsyncOnly = hasCacheOfType(testMethod, AsyncCache.class); boolean isLoadingOnly = hasCacheOfType(testMethod, LoadingCache.class) || hasCacheOfType(testMethod, AsyncLoadingCache.class) || options.compute().filter(Compute.ASYNC::equals).isPresent(); return new CacheGenerator(cacheSpec, options, isLoadingOnly, isAsyncOnly); }
Example #10
Source File: CacheContext.java From caffeine with Apache License 2.0 | 5 votes |
public <K, V> LoadingCache<K, V> build(CacheLoader<K, V> loader) { LoadingCache<K, V> cache; if (isCaffeine()) { cache = isAsync() ? caffeine.buildAsync(loader).synchronous() : caffeine.build(loader); } else { cache = new GuavaLoadingCache<>(guava.build( com.google.common.cache.CacheLoader.asyncReloading( new SingleLoader<>(loader), executor)), ticker, isRecordingStats()); } this.cache = cache; return cache; }
Example #11
Source File: LoadingCacheProxy.java From caffeine with Apache License 2.0 | 5 votes |
@SuppressWarnings("PMD.ExcessiveParameterList") public LoadingCacheProxy(String name, Executor executor, CacheManager cacheManager, CaffeineConfiguration<K, V> configuration, LoadingCache<K, Expirable<V>> cache, EventDispatcher<K, V> dispatcher, CacheLoader<K, V> cacheLoader, ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics) { super(name, executor, cacheManager, configuration, cache, dispatcher, Optional.of(cacheLoader), expiry, ticker, statistics); this.cache = cache; }
Example #12
Source File: ObjectCache.java From metron with Apache License 2.0 | 5 votes |
protected LoadingCache<String, Object> setupCache(ObjectCacheConfig config) throws IOException { LOG.info("Building ObjectCache with {}", config); return Caffeine.newBuilder().maximumSize(config.getCacheSize()) .expireAfterWrite(config.getCacheExpiration(), config.getTimeUnit()) .removalListener((path, value, removalCause) -> { LOG.debug("Object retrieved from path '{}' was removed with cause {}", path, removalCause); }) .build(new Loader(new Configuration(), config)); }
Example #13
Source File: CaffeineIntervalCollectionFactory.java From brein-time-utilities with Apache License 2.0 | 5 votes |
protected LoadingCache<String, IntervalCollection> createCache(final long cacheSize, final long expire, final TimeUnit timeUnit) { this.cacheSize = cacheSize; this.expire = expire; this.timeUnit = timeUnit; return Caffeine.newBuilder() .maximumSize(cacheSize) .expireAfterAccess(expire, timeUnit) .build(super::load); }
Example #14
Source File: GuildSettingsUtils.java From SkyBot with GNU Affero General Public License v3.0 | 5 votes |
private static GuildSettings registerNewGuild(Guild g, Variables variables, GuildSettings newGuildSettings) { final LoadingCache<Long, GuildSettings> guildSettingsCache = variables.getGuildSettingsCache(); final GuildSettings settingForGuild = guildSettingsCache.get(g.getIdLong()); if (settingForGuild != null) { return settingForGuild; } variables.getDatabaseAdapter().registerNewGuild(newGuildSettings, (bool) -> null); variables.getGuildSettingsCache().put(g.getIdLong(), newGuildSettings); return newGuildSettings; }
Example #15
Source File: CaffeineCache.java From lams with GNU General Public License v2.0 | 5 votes |
@Override 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); }
Example #16
Source File: CaffeineCache.java From java-technology-stack with MIT License | 5 votes |
@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); }
Example #17
Source File: BungeeContextManager.java From LuckPerms with MIT License | 4 votes |
private InlineQueryOptionsSupplier(ProxiedPlayer key, LoadingCache<ProxiedPlayer, QueryOptions> cache) { this.key = key; this.cache = cache; }
Example #18
Source File: ArmeriaHttpUtil.java From armeria with Apache License 2.0 | 4 votes |
private static LoadingCache<AsciiString, String> buildCache(String spec) { return Caffeine.from(spec).build(AsciiString::toString); }
Example #19
Source File: Variables.java From SkyBot with GNU Affero General Public License v3.0 | 4 votes |
public LoadingCache<Long, GuildSettings> getGuildSettingsCache() { return guildSettingsCache; }
Example #20
Source File: CaffeineUtils.java From t-io with Apache License 2.0 | 4 votes |
/** * @param cacheName * @param timeToLiveSeconds 设置写缓存后过期时间(单位:秒) * @param timeToIdleSeconds 设置读缓存后过期时间(单位:秒) * @param initialCapacity * @param maximumSize * @param recordStats * @param removalListener * @return */ public static <K, V> LoadingCache<K, V> createLoadingCache(String cacheName, Long timeToLiveSeconds, Long timeToIdleSeconds, Integer initialCapacity, Integer maximumSize, boolean recordStats, RemovalListener<K, V> removalListener) { if (removalListener == null) { removalListener = new DefaultRemovalListener<K, V>(cacheName); } Caffeine<K, V> cacheBuilder = Caffeine.newBuilder().removalListener(removalListener); //设置并发级别为8,并发级别是指可以同时写缓存的线程数 // cacheBuilder.concurrencyLevel(concurrencyLevel); if (timeToLiveSeconds != null && timeToLiveSeconds > 0) { //设置写缓存后8秒钟过期 cacheBuilder.expireAfterWrite(timeToLiveSeconds, TimeUnit.SECONDS); } if (timeToIdleSeconds != null && timeToIdleSeconds > 0) { //设置访问缓存后8秒钟过期 cacheBuilder.expireAfterAccess(timeToIdleSeconds, TimeUnit.SECONDS); } //设置缓存容器的初始容量为10 cacheBuilder.initialCapacity(initialCapacity); //设置缓存最大容量为100,超过100之后就会按照LRU最近最少使用算法来移除缓存项 cacheBuilder.maximumSize(maximumSize); if (recordStats) { //设置要统计缓存的命中率 cacheBuilder.recordStats(); } //build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存 LoadingCache<K, V> loadingCache = cacheBuilder.build(new CacheLoader<K, V>() { @Override public V load(K key) throws Exception { return null; } }); return loadingCache; // for (int i = 0; i < 20; i++) // { // //从缓存中得到数据,由于我们没有设置过缓存,所以需要通过CacheLoader加载缓存数据 // Long student = studentCache.get("p"); // System.out.println(student); // //休眠1秒 // TimeUnit.SECONDS.sleep(1); // } // System.out.println("cache stats:"); //最后打印缓存的命中率等 情况 // System.out.println(studentCache.stats().toString()); }
Example #21
Source File: GuavaCacheFromContext.java From caffeine with Apache License 2.0 | 4 votes |
GuavaLoadingCache(com.google.common.cache.LoadingCache<K, V> cache, Ticker ticker, boolean isRecordingStats) { super(cache, ticker, isRecordingStats); this.cache = requireNonNull(cache); }
Example #22
Source File: CaffeineCache.java From t-io with Apache License 2.0 | 4 votes |
private CaffeineCache(String cacheName, LoadingCache<String, Serializable> loadingCache, LoadingCache<String, Serializable> temporaryLoadingCache) { super(cacheName); this.loadingCache = loadingCache; this.temporaryLoadingCache = temporaryLoadingCache; }
Example #23
Source File: OSGiTest.java From caffeine with Apache License 2.0 | 4 votes |
@Test public void sanity() { LoadingCache<Integer, Integer> cache = Caffeine.newBuilder().build(k -> -k); assertEquals(-1, cache.get(1).intValue()); }
Example #24
Source File: CacheMetricsCollector.java From client_java with Apache License 2.0 | 4 votes |
@Override public List<MetricFamilySamples> collect() { List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>(); List<String> labelNames = Arrays.asList("cache"); CounterMetricFamily cacheHitTotal = new CounterMetricFamily("caffeine_cache_hit_total", "Cache hit totals", labelNames); mfs.add(cacheHitTotal); CounterMetricFamily cacheMissTotal = new CounterMetricFamily("caffeine_cache_miss_total", "Cache miss totals", labelNames); mfs.add(cacheMissTotal); CounterMetricFamily cacheRequestsTotal = new CounterMetricFamily("caffeine_cache_requests_total", "Cache request totals, hits + misses", labelNames); mfs.add(cacheRequestsTotal); CounterMetricFamily cacheEvictionTotal = new CounterMetricFamily("caffeine_cache_eviction_total", "Cache eviction totals, doesn't include manually removed entries", labelNames); mfs.add(cacheEvictionTotal); GaugeMetricFamily cacheEvictionWeight = new GaugeMetricFamily("caffeine_cache_eviction_weight", "Cache eviction weight", labelNames); mfs.add(cacheEvictionWeight); CounterMetricFamily cacheLoadFailure = new CounterMetricFamily("caffeine_cache_load_failure_total", "Cache load failures", labelNames); mfs.add(cacheLoadFailure); CounterMetricFamily cacheLoadTotal = new CounterMetricFamily("caffeine_cache_loads_total", "Cache loads: both success and failures", labelNames); mfs.add(cacheLoadTotal); GaugeMetricFamily cacheSize = new GaugeMetricFamily("caffeine_cache_estimated_size", "Estimated cache size", labelNames); mfs.add(cacheSize); SummaryMetricFamily cacheLoadSummary = new SummaryMetricFamily("caffeine_cache_load_duration_seconds", "Cache load duration: both success and failures", labelNames); mfs.add(cacheLoadSummary); for(Map.Entry<String, Cache> c: children.entrySet()) { List<String> cacheName = Arrays.asList(c.getKey()); CacheStats stats = c.getValue().stats(); try{ cacheEvictionWeight.addMetric(cacheName, stats.evictionWeight()); } catch (Exception e) { // EvictionWeight metric is unavailable, newer version of Caffeine is needed. } cacheHitTotal.addMetric(cacheName, stats.hitCount()); cacheMissTotal.addMetric(cacheName, stats.missCount()); cacheRequestsTotal.addMetric(cacheName, stats.requestCount()); cacheEvictionTotal.addMetric(cacheName, stats.evictionCount()); cacheSize.addMetric(cacheName, c.getValue().estimatedSize()); if(c.getValue() instanceof LoadingCache) { cacheLoadFailure.addMetric(cacheName, stats.loadFailureCount()); cacheLoadTotal.addMetric(cacheName, stats.loadCount()); cacheLoadSummary.addMetric(cacheName, stats.loadCount(), stats.totalLoadTime() / Collector.NANOSECONDS_PER_SECOND); } } return mfs; }
Example #25
Source File: ParsecAsyncHttpResponseLoadingCache.java From parsec-libraries with Apache License 2.0 | 2 votes |
/** * Returns a view of the entries stored in this cache as a synchronous {@link LoadingCache}. * A mapping is not present if the value is currently being loaded. Modifications made to the * synchronous cache directly affect the asynchronous cache. If a modification is made to a * mapping that is currently loading, the operation blocks until the computation completes. * * @return a thread-safe synchronous view of this cache */ public LoadingCache<ParsecAsyncHttpRequest, Response> synchronous() { return asyncLoadingCache.synchronous(); }
Example #26
Source File: CaffeineUtils.java From t-io with Apache License 2.0 | 2 votes |
/** * @param cacheName * @param timeToLiveSeconds 设置写缓存后过期时间(单位:秒) * @param timeToIdleSeconds 设置读缓存后过期时间(单位:秒) * @param initialCapacity * @param maximumSize * @param recordStats * @return */ public static <K, V> LoadingCache<K, V> createLoadingCache(String cacheName, Long timeToLiveSeconds, Long timeToIdleSeconds, Integer initialCapacity, Integer maximumSize, boolean recordStats) { return createLoadingCache(cacheName, timeToLiveSeconds, timeToIdleSeconds, initialCapacity, maximumSize, recordStats, null); }