什么是 "LoadingCache"?
What is a "LoadingCache"?
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.
不幸的是,上面的描述不是很清楚。 "automatically loaded" 是什么意思?
我希望这意味着:"If a requested key does not exist in the cache, it is then added to it automatically"。
以下语句(来自 get() 函数)在一定程度上支持了这一点:
"Returns the value associated with in this cache, first loading that
value if necessary."
但同样,缓存的 "loading" 方面用 "loading" 一词解释。干得好,Google :[
Guava 的 wiki 文档有一个 full definition:(重点是我的)
A LoadingCache
is a Cache
built with an attached CacheLoader
. Creating a CacheLoader
is typically as easy as implementing the method V load(K key) throws Exception
. So, for example, you could create a LoadingCache
with the following code:
[...]
The canonical way to query a LoadingCache
is with the method get(K)
. This will either return an already cached value, or else use the cache's CacheLoader
to atomically load a new value into the cache. Because CacheLoader
might throw an Exception
, LoadingCache.get(K)
throws ExecutionException
. If you have defined a CacheLoader
that does not declare any checked exceptions then you can perform cache lookups using getUnchecked(K)
; however care must be taken not to call getUnchecked
on caches whose CacheLoader
s declare checked exceptions.
强调的句子解释了所有需要解释的内容:一个值要么从缓存中取出,要么在调用get(K)
时加载。
在评论中你说你想更加强调什么是 loading。当你把东西放入缓存时,你放。当你不把东西放在缓存中,但缓存会计算并自己放入时,缓存 会为你加载。
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.
不幸的是,上面的描述不是很清楚。 "automatically loaded" 是什么意思?
我希望这意味着:"If a requested key does not exist in the cache, it is then added to it automatically"。
以下语句(来自 get() 函数)在一定程度上支持了这一点:
"Returns the value associated with in this cache, first loading that value if necessary."
但同样,缓存的 "loading" 方面用 "loading" 一词解释。干得好,Google :[
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)
时加载。
在评论中你说你想更加强调什么是 loading。当你把东西放入缓存时,你放。当你不把东西放在缓存中,但缓存会计算并自己放入时,缓存 会为你加载。