0

I'm thinking about using Caffeine for values off-loaded to disk. The keys are small, but the values may take a few megabytes. I guess, I can rely on a RemovalListener for cleaning up the mess.

For limiting the disk usage, I can use Caffeine.weigher. However, maximumWeight can not be used together with maximumSize, so it may happen that I run out of memory (because of using many cache entries) before my disk fills enough to cause eviction.

I guess, it's improbable, but possible, as there's no lower bound on the average entry size.

  • Does such a use of an in-memory cache make sense?
  • Is there a good reason why maximumSize and maximumWeight are mutually exclusive? I can imagine that both share a single slot, but saving eight bytes per cache IMHO doesn't count.
  • Is there a trick to circumvent this restriction?
maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • `maximumSize` is effectively `maximumWeight` where every entry has a weight of 1. You would set a maximum, weigh by the byte size, and and keep the metadata in memory. What would your desired behavior be? – Ben Manes Dec 18 '17 at 02:02
  • @BenManes I want two independent constraints: Limit the metadata by saying that only 1e6 entries are allowed and limit the disk space by saying that only 1e11 bytes may be used. I can get the former using `maximumSize` and the latter using `maximumWeight`, but not both. – maaartinus Dec 18 '17 at 02:31
  • If you averaged the lower bound to 1e5 it would never exceed the entry count, but could hit the byte count sooner. There probably isn’t a great solution to coerce this. – Ben Manes Dec 18 '17 at 02:49
  • @BenManes Right. I can cheat by using `Math.max(100_000, size)` or even `100_000 + size` as the weight. At the moment, I don't have any real data about the cache usage as I'm just starting, but I guess, I'll get away with the cheat. – maaartinus Dec 18 '17 at 02:59

0 Answers0