使用 Guava CacheBuilder/MapMaker 计算自定义缓存大小
我想知道目前是否有一种方法可以使用 Guava MapMaker 或使用 CacheBuilder 来提供计算缓存是否在最大大小范围内的函数?
看起来目前的驱逐只是基于缓存中的元素数量与 .maximumSize() 值相比,但是我想使用生成的映射作为位图的缓存,该位图可能包含非常小或非常大的条目。因此,我希望能够提供一个函数,根据正在使用的内存条目量计算缓存大小,然后允许缓存在此基础上逐出。
I would like to know if there is currently a way with Guava MapMaker or will be a way with CacheBuilder, to provide a function to compute whether the cache is within the maximum size?
It looks as though currently eviction is based simply on the number of elements in the cache compared to the .maximumSize() value, however I want to use the resulting map as a cache for bitmaps which could contain either very small or very large entries. Consequently I would like to be able to provide a function which computes the cache size based on the amount of memory entries are using and then allows the cache to evict on the basis of this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们目前只有
maximumSize(int)
,但正在评估maximumWeight(int, Weigher)
的需求(其中该类型只有一个Weight(K, V)
方法返回int
或double
)。重量应该是多少仍然由您决定。当然,这个功能不会在一夜之间显现出来。
We currently have only
maximumSize(int)
, but are evaluating the need formaximumWeight(int, Weigher<K, V>)
(where that type just has aweigh(K, V)
method returningint
ordouble
). It's still up to you to come up with what the weight should be.Of course, this feature won't show up overnight.
似乎来自 CacheBuilder 无法指定 CacheBuilder 类和 MapMaker 类的最大内存使用量。您只能设置最大元素数。
EhCache 等缓存框架允许您指定缓存使用的堆(和/或磁盘空间)的最大大小。在 EhCache 中,您可以使用 maxBytesLocalHeap 和/或 maxBytesLocalDisk 构建器方法来指定这种行为。有关详细信息,请参阅 CacheConfiguration 的 JavaDocs。
It seems from the JavaDocs for CacheBuilder that there is no way of specifying the maximum memory usage for neither the CacheBuilder class nor MapMaker class. You can only set the maximum number of elements.
Cache frameworks such as EhCache does allow you to specify the maxium size of the heap (and/or disk space) to be used by cache. In EhCache you may use the maxBytesLocalHeap and/or the maxBytesLocalDisk builder methods to specify this kind of behaviour. Consult the JavaDocs for CacheConfiguration for more information.