Sneaky Little Snippets

Using Google Guava For Cache With Timeout – Java

If you need a list to keep your objects for a while and then automatically delete/expire them, you can use caches from Google Guava.

The Guava project contains several of Google’s core libraries that we rely on in our Java-based projects: collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, and so forth.

This abstract class gives us freedom to create different types of caches with different behavior to decide what is going to happen to expired items.
Now let’s create a class that we can use as timeout list.