How to invalidate guava cache at scheduled time?

Viewed 362

I have a requirement in which it is required to invalidate or cleanup a cache every day around the fixed time. I can schedule a daily job to invalidate the cache at the fixed time. However, I am not looking for that.

Is there any other simpler approach to invalidate the cache like some in-built methods or are there any other suggestions?

1 Answers

You can use expireAfterAccess.

It specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation

expireAfterAccess(long duration, TimeUnit unit)

If you want to invalidate at specified time, you can create a schedular thread that runs at specific time and calls invalidateAll()

Related