caching - 什么是 "LoadingCache"?

标签 caching guava

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 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) 时加载。

<小时/>

在评论中,您说您希望更多地强调正在加载的内容。当将内容放入缓存时,您放入了。当您没有将内容放入缓存中,但缓存会自行计算并放入内容时,缓存会为您加载它。

关于caching - 什么是 "LoadingCache"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43993731/

相关文章:

gradle - Protobuf/ Guava 依赖性问题

caching - Yii 缓存在控制台应用程序和网络应用程序之间共享缓存?

php - magento 如何得出缓存键名?

python - 如何从 webdriver 访问 Firefox 缓存?

scala - 为什么我需要jsr305才能在scala中使用guava?

java - 为什么不同组织的javax.annotation不同

c - OpenMP - 没有错误共享的数组插入

c++ - atoi() 和 atof() 缓存吗?它们似乎执行得越快,调用的次数越多

java - Guava CacheBuilder 在缓存过期后不会立即调用removalListener

java - Guava checkNotNull 有什么意义