I have a redis caching server. If that caching server is down, my app will query the database directly.
I want to know from the logs if my caching server is not reachable. However, if I add a log error, it would completely spam my logfiles if a high amount of users would try to access data.
As a result, I would like to only log the very first error occurrence, resetting only if no errors occurred for an hour.
Is this a feasable approach at all? If so, please review my implementation:
public class CustomCacheErrorHandler implements CacheErrorHandler {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private long errorOccurred;
private final long ONEHOUR = 3600000;
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
if (errorOccurred < (System.currentTimeMillis() - ONEHOUR))
logger.error("Error while getting cache " + cache.getName() + " for Key " + key);
errorOccurred = System.currentTimeMillis()
}
}
Using currentTimeMillis over System.nanoTime() and a final long ONEHOUR over TimeUnit.HOURS.toMillis(1) for performance.
Thanks in advance!