HashMap with TTL


We were working on a proof of concept and for that I quickly needed a HashMap with a Time to Live (TTL). Basically, I wanted the keys to expire automatically after a certain time period.

Fortunatly, Google Guava provides this out of the box in form of LoadingCache

Here is how you would initialize it,

private LoadingCache<String, String> cache = CacheBuilder.newBuilder()
            .maximumSize(Integer.MAX_VALUE)
            .expireAfterWrite(5, TimeUnit.MINUTES)
            .build(new CacheLoader<String, String>() {
                @Override
                public String load(final String response) throws Exception {
                    return response;
                }
            });

You can specify the maximum size of cache, after which the entries will be automatically evicted according to Least Recently Used (LRU) algorithm.

The TTL can be specified using expireAfterWrite method.

Note that when using this implementation, do not rely on the size() property of the map since that might contain the expired entries as well. However, they will not be retrievable using the get method.

The expired entires are removed after a certain time duration using a routine maintainence job.