简体   繁体   English

如何禁用Guava缓存?

[英]How to disable Guava caching?

JavaCachingWithGuava suggests the canonical way to turn off caching is to set maximumSize=0 . JavaCachingWithGuava建议关闭缓存的规范方法是设置maximumSize = 0 However, I'd expect the test below to pass: 但是,我希望下面的测试通过:

public class LoadingCacheTest {
    private static final Logger LOG = LoggerFactory.getLogger(LoadingCacheTest.class);

    LoadingCache<String, Long> underTest;

    @Before
    public void setup() throws Exception {
        underTest = CacheBuilder.from("maximumSize=0").newBuilder()
                .recordStats()
                .removalListener(new RemovalListener<String, Long>() {
                    @Override
                    public void onRemoval(RemovalNotification<String, Long> removalNotification) {
                        LOG.info(String.format("%s cached value %s for key %s is evicted.", removalNotification.getCause().name(), removalNotification.getValue(), removalNotification.getKey()));
                    }
                })
                .build(new CacheLoader<String, Long>() {
                    private final AtomicLong al = new AtomicLong(0);
                    @Override
                    public Long load(@NotNull final String key) throws Exception {
                        LOG.info(String.format("Cache miss for key '%s'.", key));
                        return al.incrementAndGet();
                    }
                });
    }

    @Test
    public void testAlwaysCacheMissIfCacheDisabled() throws Exception {
        String key1 = "Key1";
        String key2 = "Key2";
        underTest.get(key1);
        underTest.get(key1);
        underTest.get(key2);
        underTest.get(key2);
        LOG.info(underTest.stats().toString());
        Assert.assertThat(underTest.stats().missCount(), is(equalTo(4L)));
    }
}

That is, turning off the cache always results in a cache miss. 也就是说,关闭缓存总是会导致缓存未命中。

The test fails, though. 但测试失败了。 Is my interpretation incorrect? 我的解释不正确吗?

The method newBuilder constructs a new CacheBuilder instance with default settings, ignoring the call to from . newBuilder方法使用默认设置构造一个新的CacheBuilder实例,忽略对from的调用。 However, you want to construct a CacheBuilder with a specific maximum size. 但是,您希望构造具有特定最大大小的CacheBuilder So, remove the call to newBuider . 因此,删除对newBuider的调用。 You should use your call to build to get a CacheBuilder matching your spec rather than getting one with default settings: 你应该使用你的调用来build一个匹配你的规范的CacheBuilder而不是使用默认设置:

underTest = CacheBuilder.from("maximumSize=0")
                .recordStats()
                .removalListener(new RemovalListener<String, Long>() {
                    @Override
                    public void onRemoval(RemovalNotification<String, Long> removalNotification) {
                        LOG.info(String.format("%s cached value %s for key %s is evicted.", removalNotification.getCause().name(), removalNotification.getValue(), removalNotification.getKey()));
                    }
                })
                .build(new CacheLoader<String, Long>() {
                    private final AtomicLong al = new AtomicLong(0);
                    @Override
                    public Long load(@NotNull final String key) throws Exception {
                        LOG.info(String.format("Cache miss for key '%s'.", key));
                        return al.incrementAndGet();
                    }
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM