Guava入门~CacheStats

构建缓存对象时调用recordStats(),指定可以做缓存统计

LoadingCache<String,TradeAccount> tradeAccountCache = CacheBuilder.newBuilder()
		.recordStats()

获取缓存统计结果

CacheStats cacheStats = cache.stats();

示例

package bbejeck.guava.chapter6.cache;

import bbejeck.guava.common.model.TradeAccount;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;

/**
 * User: Bill Bejeck
 * Date: 7/22/13
 * Time: 9:55 PM
 */
public class CacheStatsTest {
    private CacheLoader<String, TradeAccount> cacheLoader = mock(CacheLoader.class);

    @Test
    public void testGetCacheStats() throws Exception {
        TradeAccount expectedTradeAccount = new TradeAccount.Builder().build();
        when(cacheLoader.load("223")).thenReturn(expectedTradeAccount);
        when(cacheLoader.load("000")).thenThrow(new RuntimeException("Bad Value"));
        LoadingCache<String, TradeAccount> tradeAccountCache = CacheBuilder.newBuilder()
                .maximumSize(5000L)
                .expireAfterAccess(10l, TimeUnit.MILLISECONDS)
                .recordStats()
                .build(cacheLoader);

        TradeAccount tradeAccount = tradeAccountCache.get("223");
        assertThat(tradeAccount, is(expectedTradeAccount));
        TradeAccount tradeAccount1 = tradeAccountCache.get("223");
        assertThat(tradeAccount1, is(expectedTradeAccount));
        Thread.sleep(20);
        TradeAccount tradeAccount2 = tradeAccountCache.get("223");
        assertThat(tradeAccount2, is(expectedTradeAccount));
        try{
        tradeAccountCache.get("000");
        }catch (RuntimeException e){
            //Ignore expected
        }
        CacheStats stats = tradeAccountCache.stats();

        assertThat(stats.evictionCount(),is(1l));
        assertThat(stats.hitCount(),is(1l));
        assertThat(stats.missCount(),is(3l));
        assertThat(stats.hitRate(),is((double)1/4));
        assertThat(stats.loadExceptionCount(),is(1l));
        assertThat(stats.loadExceptionRate(),is((double)1/3));
        assertThat(stats.loadSuccessCount(),is(2l));

        verify(cacheLoader, times(2)).load("223");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值