什么是 "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 CacheLoaders declare checked exceptions.

强调的句子解释了所有需要解释的内容:一个值要么从缓存中取出,要么在调用get(K)时加载。


在评论中你说你想更加强调什么是 loading。当把东西放入缓存时,你。当你不把东西放在缓存中,但缓存会计算并自己放入时,缓存 会为你加载