Let's say I have value1 (that was created 1min ago), value2 (that was created 5mins ago), and value3 (created 15mins ago).
I want to add to cache the values for the last 10 minutes. How to do that in scala? I'm only seeing maximum size but not maximum time in Google Guava CacheLoader.
In other words, I want to get value1 and value2 since they pass the criteria "Last 10 minutes" and evict value3 as it was created 15mins ago.
Guava caches support timed eviction.
For your usage scenario, you could use a CacheBuilder
to create a cache that expires entries after 10 minutes using the expireAfterWrite(long duration, TimeUnit unit)
method. (Use 10
for the duration
and TimeUnit.MINUTES
for the unit
parameters.)
The javadoc clearly states that
[e]xpired entries [...] will never be visible to read or write operations
so this should completely satisfy your use case.