开发者

Compute custom cache size with Guava CacheBuilder/MapMaker

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 th开发者_C百科e basis of this.


We currently have only maximumSize(int), but are evaluating the need for maximumWeight(int, Weigher<K, V>) (where that type just has a weigh(K, V) method returning int or double). It's still up to you to come up with what the weight should be.

Of course, this feature won't show up overnight.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜