0

I'm using Guava's cache in my program. I expected Guava may use a timer thread to expire its items, but I checked the source code and it seems Guava would clear its expired items only when program tries to access its items. I'm not sure if that's the way Guava's cache works.

Here's my requirement. My program works with several huge objects which loaded from files. Once a huge object get in use, the object may be accessed several times in consecutive several minutes, and then the cache would not be accessed at all in a long time. I prefer the object can be cleared as soon as possible just as expireAfterAccess(10min) or clearedAfterAccess(10min).

Master Qiao
  • 301
  • 2
  • 10
  • Guava waits until enough other activity on the cache (reads, writes) occurs, which triggers a maintenance cycle to amortize eviction. For a cache with modest usage this is fairly prompt, but one that is mostly idle will be delayed. For those cases you can periodically call `Cache.cleanUp()` in a scheduled thread. Guava's successor, Caffeine, allows you to specify a [Scheduler](https://github.com/ben-manes/caffeine/wiki/Cleanup) thread if you need prompt removal. – Ben Manes Jan 16 '22 at 04:30
  • That's one of those 'you should have written the text in the box that's a bit lower on the page, mate' comments, @BenManes. All there is left to say is that Caffeine is _just_ the caching part. Which is a good thing, if that's all you're interested in! – rzwitserloot Jan 16 '22 at 04:54

0 Answers0