当前位置: 首页>>代码示例>>Java>>正文


Java LoadingCache.get方法代码示例

本文整理汇总了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);
}
 
开发者ID:micrometer-metrics,项目名称:micrometer,代码行数:25,代码来源:CaffeineCacheMetricsTest.java

示例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);
}
 
开发者ID:prometheus,项目名称:client_java,代码行数:33,代码来源:CacheMetricsCollectorTest.java


注:本文中的com.github.benmanes.caffeine.cache.LoadingCache.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。