-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Cleanup
Caffeine caches do not perform cleanup and evict values "automatically" or instantly after a value expires, or anything of the sort. Instead, it performs small amounts of maintenance during write operations or during occasional read operations if writes are rare.
The reason for this is as follows: if we wanted to perform cache maintenance continuously we would need to lock the data structure on every operation or create a thread. Some environments restrict the creation of threads, which would make Caffeine unusable in that environment.
Instead, we put the choice in your hands. If your cache is high-throughput, then you don't have to worry about performing cache maintenance to clean up expired entries and the like. If your cache does writes only rarely and you don't want cleanup to block cache reads, you may wish to create your own maintenance thread that calls cache.cleanUp()
at regular intervals.
If you want to schedule regular cache maintenance for a cache which only rarely has writes, just schedule the maintenance using ScheduledExecutorService.