管理 Google 番石榴缓存中的条目
Manage entries in Google Guava Cache
我需要开发一个带有缓存(Guava 缓存)的嵌入式数据库(Derby)的Spring 启动微服务.
每个客户的条目大小不同。
- 如果我不设置
maximumSize
,会发生什么?大小会不断增长吗?它会影响尺寸吗?内存?
- 如果我安排一个预先形成
cleanUp
的作业(对于每个间隔),条目何时会从缓存中删除?以及如果我只有 refreshAfterWrite
? 会有什么影响
- 假设我设置了
refreshAfterWrite
而没有调用 cleanUp
,过期的条目会从缓存中删除吗? (即,我将调用 cache.size()
并查看它是否已减少)。
谢谢
问题太多了。在 Guava 的 CachesExplained wiki page 上都有解释。
If I'll not set maximumSize
, what will happened? the size will grow constantly?
当满足其他条件或不再有可用内存时,条目将被释放。所以是的,只要不满足这些条件,规模就会增长。 (节"Size-based Eviction")
it will effected the size? memory?
maximumSize(long)
指定特定的对象大小。默认情况下,它是缓存中存在的元素数。但是如果你使用weigher(Weigher)
,你可以说一个特定的元素值或多或少,从而使它更接近实际的内存使用。但是你必须自己写Weigher
,然后。 (节"Size-based Eviction")
If I'll scheduled a job that preform cleanUp
(for each interval), when the entries will remove from the cache?
当您调用 cleanUp()
方法时,条目将被清除。 (第"When Does Cleanup Happen?"节)
and how it will effected if I've only refreshAfterWrite
?
如果您使用 refreshAfterWrite
缓存调用 cleanUp()
,无论 refreshAfterWrite
是否被触发,您希望访问的条目都必须重新加载。
Lets say I set refreshAfterWrite
without any call to cleanUp, the the expired entries will removed from the cache?
不,refreshAfterWrite
与 expireAfterWrite
不同。该条目将有资格刷新,但不会删除。如果您使用 expireAfterWrite
,则该条目将过期并被 删除。
(i.e., I'll call to cache.size() and see that it has been decreased).
事情不是这样的。缓存不是有保证的工作人员。它不像一个集合。计算大小时可能没有考虑某些条目,或者某些条目仍算作大小的一部分,但尚未有效删除。这是明确的 written in the Javadoc for the size()
method:
size
long size()
Returns the approximate number of entries in this cache.
基本上,您真的应该阅读 Guava 的整个文档 CachesExplained wiki page。所有实际信息都在那里,以一种非常具有描述性的方式。
我需要开发一个带有缓存(Guava 缓存)的嵌入式数据库(Derby)的Spring 启动微服务.
每个客户的条目大小不同。
- 如果我不设置
maximumSize
,会发生什么?大小会不断增长吗?它会影响尺寸吗?内存? - 如果我安排一个预先形成
cleanUp
的作业(对于每个间隔),条目何时会从缓存中删除?以及如果我只有refreshAfterWrite
? 会有什么影响
- 假设我设置了
refreshAfterWrite
而没有调用cleanUp
,过期的条目会从缓存中删除吗? (即,我将调用cache.size()
并查看它是否已减少)。
谢谢
问题太多了。在 Guava 的 CachesExplained wiki page 上都有解释。
If I'll not set
maximumSize
, what will happened? the size will grow constantly?
当满足其他条件或不再有可用内存时,条目将被释放。所以是的,只要不满足这些条件,规模就会增长。 (节"Size-based Eviction")
it will effected the size? memory?
maximumSize(long)
指定特定的对象大小。默认情况下,它是缓存中存在的元素数。但是如果你使用weigher(Weigher)
,你可以说一个特定的元素值或多或少,从而使它更接近实际的内存使用。但是你必须自己写Weigher
,然后。 (节"Size-based Eviction")
If I'll scheduled a job that preform
cleanUp
(for each interval), when the entries will remove from the cache?
当您调用 cleanUp()
方法时,条目将被清除。 (第"When Does Cleanup Happen?"节)
and how it will effected if I've only
refreshAfterWrite
?
如果您使用 refreshAfterWrite
缓存调用 cleanUp()
,无论 refreshAfterWrite
是否被触发,您希望访问的条目都必须重新加载。
Lets say I set
refreshAfterWrite
without any call to cleanUp, the the expired entries will removed from the cache?
不,refreshAfterWrite
与 expireAfterWrite
不同。该条目将有资格刷新,但不会删除。如果您使用 expireAfterWrite
,则该条目将过期并被 删除。
(i.e., I'll call to cache.size() and see that it has been decreased).
事情不是这样的。缓存不是有保证的工作人员。它不像一个集合。计算大小时可能没有考虑某些条目,或者某些条目仍算作大小的一部分,但尚未有效删除。这是明确的 written in the Javadoc for the size()
method:
size
long size()
Returns the approximate number of entries in this cache.
基本上,您真的应该阅读 Guava 的整个文档 CachesExplained wiki page。所有实际信息都在那里,以一种非常具有描述性的方式。