HashMap with TTL
Posted on 27 Feb 2021
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...