gpt4 book ai didi

guava - 如何处理 Google Guava Cache 刷新异常?

转载 作者:行者123 更新时间:2023-12-04 14:16:26 35 4
gpt4 key购买 nike

Google Guava Cache 文档指出:

Refreshing is not quite the same as eviction. As specified in LoadingCache.refresh(K), refreshing a key loads a new value for the key, possibly asynchronously. The old value (if any) is still returned while the key is being refreshed, in contrast to eviction, which forces retrievals to wait until the value is loaded anew.

If an exception is thrown while refreshing, the old value is kept, and the exception is logged and swallowed.



这种记录和吞咽异常在我的用例中非常糟糕,因为这意味着如果刷新引发异常,缓存的用户将继续在缓存中找到陈旧的数据。

如何确保刷新时引发异常,缓存开始返回 null 或调用 load 方法?

最佳答案

如果您不想提供过时的数据,您应该调用 invalidate(key) 而不是 refresh(key) .这将丢弃 key 的缓存值,如果存在的话。

然后调用 get(key)将同步委托(delegate)给值加载器,并重新抛出 CacheLoader 抛出的任何异常。 , 包裹在 (Unchecked)ExecutionException 中.

关于guava - 如何处理 Google Guava Cache 刷新异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11641512/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com