Google 提供了“loading cache ”,其描述如下:
A semi-persistent mapping from keys to values. Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.
不幸的是,上面的描述不是很清楚。 “自动加载”是什么意思?
我希望这意味着:“如果缓存中不存在请求的 key ,则会自动将其添加到缓存中”。
这个语句(来自 get() 函数)在某种程度上支持了这一点:
"Returns the value associated with in this cache, first loading that value if necessary."
但同样,缓存的“加载”方面是用“加载”一词来解释的。一路走好,谷歌:[
最佳答案
Guava 的 wiki 文档有 full definition其中:(重点是我的)
A
LoadingCache
is aCache
built with an attachedCacheLoader
. Creating aCacheLoader
is typically as easy as implementing the methodV load(K key) throws Exception
. So, for example, you could create aLoadingCache
with the following code:[...]
The canonical way to query a
LoadingCache
is with the methodget(K)
. This will either return an already cached value, or else use the cache'sCacheLoader
to atomically load a new value into the cache. BecauseCacheLoader
might throw anException
,LoadingCache.get(K)
throwsExecutionException
. If you have defined aCacheLoader
that does not declare any checked exceptions then you can perform cache lookups usinggetUnchecked(K)
; however care must be taken not to callgetUnchecked
on caches whoseCacheLoader
s declare checked exceptions.
强调的这句话解释了所有需要解释的内容:一个值要么从缓存中获取,要么在调用 get(K)
时加载。
在评论中,您说您希望更多地强调正在加载的内容。当您将内容放入缓存时,您放入了。当您没有将内容放入缓存中,但缓存会自行计算并放入内容时,缓存会为您加载它。
关于caching - 什么是 "LoadingCache"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43993731/