本文整理汇总了Java中com.github.benmanes.caffeine.cache.LoadingCache.get方法的典型用法代码示例。如果您正苦于以下问题:Java LoadingCache.get方法的具体用法?Java LoadingCache.get怎么用?Java LoadingCache.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.benmanes.caffeine.cache.LoadingCache
的用法示例。
在下文中一共展示了LoadingCache.get方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadingCacheExposesMetricsForLoadsAndExceptions
import com.github.benmanes.caffeine.cache.LoadingCache; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Test
void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception {
LoadingCache<Integer, String> cache = CaffeineCacheMetrics.monitor(registry, Caffeine.newBuilder()
.recordStats()
.build(key -> {
if (key % 2 == 0)
throw new Exception("no evens!");
return key.toString();
}), "c", userTags);
cache.get(1);
cache.get(1);
try {
cache.get(2); // throws exception
} catch (Exception ignored) {
}
cache.get(3);
assertThat(registry.mustFind("c.requests").tags("result", "hit").tags(userTags).functionCounter().count()).isEqualTo(1.0);
assertThat(registry.mustFind("c.requests").tags("result", "miss").tags(userTags).functionCounter().count()).isEqualTo(3.0);
assertThat(registry.mustFind("c.load").tags("result", "failure").functionCounter().count()).isEqualTo(1.0);
assertThat(registry.mustFind("c.load").tags("result", "success").functionCounter().count()).isEqualTo(2.0);
}
示例2: loadingCacheExposesMetricsForLoadsAndExceptions
import com.github.benmanes.caffeine.cache.LoadingCache; //导入方法依赖的package包/类
@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);
}