Rupert Bates
Rupert Bates

Reputation: 3061

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 the basis of this.

Upvotes: 2

Views: 1452

Answers (2)

Kevin Bourrillion
Kevin Bourrillion

Reputation: 40851

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.

Upvotes: 5

Zapodot
Zapodot

Reputation: 462

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.

Upvotes: 3

Related Questions