I have a Guava Cache with a CacheLoader
. There is an external condition I track in a Thread, and if this happens I want to refresh()
all entries asynchronously. For this reason I do not use invalidateAll()
since the next get()
would have to wait for load to succeed.
我有一个带有CacheLoader的Guava缓存。我在一个线程中跟踪一个外部条件,如果发生这种情况,我想异步刷新()所有条目。因此我不使用invalidateAll(),因为下一个get()必须等待加载成功。
Instead I iterate over the keys contained in the cache and refresh(k)
all of them, as I could not find a refreshAll() method. Here is the code (but not really related to the question):
相反,我遍历缓存中包含的键并刷新(k)所有这些键,因为我找不到refreshAll()方法。这是代码(但与问题无关):
Set<ResourceLoaderKey> keys = resourceLoaderCache.asMap().keySet();
for(ResourceLoaderKey k : keys)
{
resourceLoaderCache.refresh(k);
}
My problem is now, that reload()
in the CacheLoader might detect, that a resource is actually not any longer available. Currently the reload()
throws a ResourceNotFound exception which works for the get()
case. But it does not work for the refresh()
case, as the old value will be served as long as the reload()
fails.
我现在的问题是,CacheLoader中的reload()可能会检测到资源实际上不再可用。目前reload()抛出一个适用于get()情况的ResourceNotFound异常。但它不适用于refresh()情况,因为只要reload()失败就会提供旧值。
I can now trap the not-found exception in the load/reload methods and invalidate the entry somehow, but I wonder if there is a official way to do it (returning null or a null future is logged as a warning and ignored)? It would be good to be able to remove the key/absent-value instead of keeping a placeholder object around.
我现在可以在加载/重新加载方法中捕获未找到的异常并以某种方式使条目无效,但我想知道是否有正式方法(返回null或null将被记录为警告并被忽略)?能够删除键/缺席值而不是保留占位符对象会很好。
1 个解决方案
#1
0
A supported way to do this doesn't currently exist in guava r17.
番石榴r17目前不支持这种方法。
Have you tried getting all the keys, then invalidating all, then attempting to get all the keys you had saved?
您是否尝试过获取所有密钥,然后使所有密钥无效,然后尝试获取您保存的所有密钥?
Set<ResourceLoaderKey> keys = resourceLoaderCache.asMap().keySet();
resourceLoaderCache.invalidateAll(keys);
for(ResourceLoaderKey k : keys) {
resourceLoaderCache.get(k); //or getUnchecked() whichever you prefer
}
That should make sure no stale values stay in the cache, and recache all values that exist.
这应该确保没有过时的值保留在缓存中,并重新存储所有存在的值。
(My problem was slightly different, I had to change my assumptions to solve mine)
(我的问题略有不同,我不得不改变我的假设来解决我的问题)
#1
0
A supported way to do this doesn't currently exist in guava r17.
番石榴r17目前不支持这种方法。
Have you tried getting all the keys, then invalidating all, then attempting to get all the keys you had saved?
您是否尝试过获取所有密钥,然后使所有密钥无效,然后尝试获取您保存的所有密钥?
Set<ResourceLoaderKey> keys = resourceLoaderCache.asMap().keySet();
resourceLoaderCache.invalidateAll(keys);
for(ResourceLoaderKey k : keys) {
resourceLoaderCache.get(k); //or getUnchecked() whichever you prefer
}
That should make sure no stale values stay in the cache, and recache all values that exist.
这应该确保没有过时的值保留在缓存中,并重新存储所有存在的值。
(My problem was slightly different, I had to change my assumptions to solve mine)
(我的问题略有不同,我不得不改变我的假设来解决我的问题)