We have a simple but very much used cache, implemented by a ConcurrentHashMap. Now we want to refresh all values at regular times (say, every 15 minutes).
I would like code like this:
private void regularCacheCleanup() {
final long now = System.currentTimeMillis();
final long delta = now - cacheCleanupLastTime;
if (delta < 0 || delta > 15 * 60 * 1000) {
cacheCleanupLastTime = now;
clearCache();
}
}
Except it should be:
- Thread safe
- Non-blocking and extremely performant if the cache isn’t going to be cleared
- No dependencies except on java.* classes (so no Google CacheBuilder)
- Rock-solid 😉
- Can’t start new threads
Right now I think to implement a short timer in a ThreadLocal. When this expires, the real timer will be checked in a synchronized way. That’s an awfull lot of code, however, so a more simple idea would be nice.
The mainstream way to tackle this issue would be by using some timer thread to refresh your cache on specified intervals. However, since you don’t need to create new threads, a possible implementation that i can think of is that of a pseudo-timed cache refresh. Basically, i would insert checks in cache accessors (put and get methods) and each time clients would use this methods, i would check if the cache needs to be refreshed before performing the put or get action. This is the rough idea:
Personally i wouldn’t consider doing it like so, but if you don’t want or can’t create timer threads then this could be an option for you.
Note that although this implementation avoids locks, it is still prone to duplicate refreshes due to race events. If this is ok for your requirements then it should be no problem. If however you have stricter requirements then you need to put locking in order to properly synchronise the threads and avoid race events.