Java Code Examples for com.github.benmanes.caffeine.cache.LoadingCache#get()
The following examples show how to use
com.github.benmanes.caffeine.cache.LoadingCache#get() .
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: 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 4
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 5
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); }