Home > Back-end >  How the Guava cache achieve remove the key when the ttl is arriving?
How the Guava cache achieve remove the key when the ttl is arriving?

Time:11-22

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)

CodePudding user response:

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

  • Related