Reputation: 1344
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:
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.
Upvotes: 3
Views: 3632
Reputation: 2322
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:
class YourCache {
// holds the last time the cache has been refreshed in millis
private volatile long lastRefreshDate;
// indicates that cache is currently refreshing entries
private volatile boolean cacheCurrentlyRefreshing;
private Map cache = // Your concurrent map cache...
public void put(Object key, Object element) {
if (cacheNeedsRefresh()) {
refresh();
}
map.put(key, element);
}
public Object get(Object key) {
if (cacheNeedsRefresh()) {
refresh();
}
return map.get(key);
}
private boolean cacheNeedsRefresh() {
// make sure that cache is not currently being refreshed by some
// other thread.
if (cacheCurrentlyRefreshing) {
return false;
}
return (now - lastRefreshDate) >= REFRESH_INTERVAL;
}
private void refresh() {
// make sure the cache did not start refreshing between cacheNeedsRefresh()
// and refresh() by some other thread.
if (cacheCurrentlyRefreshing) {
return;
}
// signal to other threads that cache is currently being refreshed.
cacheCurrentlyRefreshing = true;
try {
// refresh your cache contents here
} finally {
// set the lastRefreshDate and signal that cache has finished
// refreshing to other threads.
lastRefreshDate = System.currentTimeMillis();
cahceCurrentlyRefreshing = false;
}
}
}
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.
Upvotes: 5