sreedhar
sreedhar

Reputation: 316

Guava maximumSize or maximumWeight for limiting cache memory size

I'm trying to figure out what the number that you specify in Guava CacheBuilder maximumSize() and maximumweight(),weigher represent.

Say I've got something like this in my code,

Cache<String, Object> programCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.build();

Does the 1000 that I specified as the max size mean that I can have a thousand different entries in the cache before it starts kicking out the LRU (no matter what size the object might be)? If this is the case, is there a limit to the size of the object?

Or does that 1000 mean, that I have a 1000mb(is MB correct?) to work with and I can have as many of the Objects in the cache as I want up to 1000mb before it starts kicking out the LRU?

Then what exactly maximumweight is?

Appreciated if some good configuration is provided for limiting Guava cache memory of 500MB.

Upvotes: 0

Views: 3586

Answers (2)

wongoo
wongoo

Reputation: 666

  • the count of entries can't exceed maximumSize, otherwise evict by LRU.
  • The sum of weight(return by weigher) of all entries can't exceed maximumWeight, otherwise evict by LRU.
  • Can't use maximumSize and maximumWeight at one time.

Upvotes: 0

crusy
crusy

Reputation: 1512

Docs say maximumSize(long)

Specifies the maximum number of entries the cache may contain.

Specifying 500MB should be possible with maximumWeight(long) and a custom Weigher, as

There is no unit for entry weights; rather they are simply relative to each other.

Upvotes: 1

Related Questions