0

Recently,I start using guava cache in my project.But when I see the source code , I don't find the code where key removed when the ttl is arriving.

Can someone guide me a little bit? Thanks a lot. (My English is not so good)

Qifan.y
  • 3
  • 1
  • I don't have the code handy but I'd assume it's 1 of 2 things: either there is a background thread that's evicting entries which are past their ttl or ttl is handled when trying to lookup entries or trying to free space if the cache gets full. – Thomas Nov 22 '22 at 07:23
  • Ahh! I agree with you. And I just want to find the code in source code removing the expired key – Qifan.y Nov 22 '22 at 07:41
  • Possible duplicate of https://stackoverflow.com/q/10144194/869736 – Louis Wasserman Nov 22 '22 at 16:31

1 Answers1

2

According to the documentation:

Caches built with CacheBuilder 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 create a thread, and its operations would be competing with user operations for shared locks. Additionally, some environments restrict the creation of threads, which would make CacheBuilder unusable in that environment.

Source: https://github.com/google/guava/wiki/CachesExplained#when-does-cleanup-happen

Markus
  • 1,649
  • 1
  • 22
  • 41