caching - Google Guava Cache 在刷新同一键的值时是否进行重复数据删除

标签 caching guava google-guava-cache

我使用 Google Guava 实现了一个非阻塞缓存,缓存中只有一个键,并且该键的值仅异步刷新(通过重写 reload())。

我的问题是,如果第一个 reload() 任务尚未完成,并且出现新的 get() 请求,Guava 缓存是否会处理重复数据删除。

    //Cache is defined like below
    this.cache = CacheBuilder
            .newBuilder()
            .maximumSize(1)
            .refreshAfterWrite(10, TimeUnit.MINUTES)
            .recordStats()
            .build(loader);

//reload is overwritten asynchronously
@Override
public ListenableFuture<Map<String, CertificateInfo>> reload(final String key, Map<String, CertificateInfo> prevMap) throws IOException {
    LOGGER.info("Refreshing certificate cache.");
    ListenableFutureTask<Map<String, CertificateInfo>> task = ListenableFutureTask.create(new Callable<Map<String, CertificateInfo>>() {
        @Override
        public Map<String, CertificateInfo> call() throws Exception {
            return actuallyLoad();
        }
    });
    executor.execute(task);
    return task;
}

最佳答案

是的,请参阅 LoadingCache.get(K) 的文档(和它的兄弟,Cache.get(K, Runnable)):

If another call to get(K) or getUnchecked(K) is currently loading the value for key, simply waits for that thread to finish and returns its loaded value.

因此,如果当前正在计算(或重新加载/重新计算)缓存条目,则尝试检索该条目的其他线程将只是等待计算完成 - 它们不会启动自己的冗余刷新。

关于caching - Google Guava Cache 在刷新同一键的值时是否进行重复数据删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083174/

相关文章:

http - 为什么要在 HTTP 响应中同时使用 no-cache 和 no-store?

scala - 使跨多个服务器的弱 HashMap 无效

hadoop - Apache Ignite:如何在服务器节点关闭时将客户端节点的连接实例设置为 'NULL'打开?

java - 具有重复键的映射实现

java - 为什么Guava LoadingCache的getAll方法返回不同类型的ImmutableMap?

jakarta-ee - 谷歌缓存可以在集群环境中工作吗

c++ - 我应该缓存用作哈希键的 STL 字符串的哈希码吗?

java - Guava 和 SQL : cast int to Ints

android - Android Gradle插件3.0.1-将字节码转换为dex时出错

java - Guava CacheLoader 抛出并捕获自定义异常