java - 如何禁用 Guava 缓存?

标签 java guava

JavaCachingWithGuava建议关闭缓存的规范方法是设置 maximumSize=0。 但是,我希望下面的测试能够通过:

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)));
    }
}

也就是说,关闭缓存总是会导致缓存未命中。

不过,测试失败了。我的解释不正确吗?

最佳答案

方法newBuilder构造一个新的 CacheBuilder具有默认设置的实例,忽略对 from 的调用.但是,您想构造一个 CacheBuilder具有特定的最大尺寸。因此,删除对 newBuider 的调用.您应该调用 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();
                    }
                });

关于java - 如何禁用 Guava 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13763211/

相关文章:

java - 将控制台与 Eclipse 中的源代码链接

Java Decompiler 生成抽象枚举

android - java.lang.NoClassDefFoundError : Failed resolution of: App class on Lenovo K4 Note running android 6, API 23

java - 由于 Java 泛型,我无法编译 Guava 的缓存构建器

java - ActiveMQ 浏览器需要很长时间才能执行最后一个 .hasMoreElements()

java - 为什么我的玩家在释放箭头键后继续移动(非常缓慢)?

Java-从 LDAP 检索 shiro 的权限

java - 如何在 Guava 订购中找到哪个比较器 'broke the tie'

guava - Kotlin:使用 google-guava 静态方法作为扩展

JAVA Guava /集合: How to use a predefined list as a predicate to sort another list by?