public final class IgnoreExceptionsCacheManagerDecorator implements CacheManager { private static final Object NULL = new Object(); private static Logger logger = Logger.getAnonymousLogger(); private final CacheManager decorated; public IgnoreExceptionsCacheManagerDecorator(CacheManager decorated) { this.decorated = decorated; } public Object getFromCache(String key) { try { return decorated.getFromCache(key); } catch (Exception e) { logger.log(SEVERE, "Unable to retrieve an object using key \"" + key + "\"", e); } return NULL; } public void putInCache(String key, Object o) { try { decorated.putInCache(key, o); } catch (Exception e) { logger.log(SEVERE, "Unable to store the object " + o + " using key \"" + key + "\"", e); } } }为了避免缓存系统中的任何错误导致产品中某些应用程序停止运行,我们仅需要使用IgnoreExceptionsCacheManagerDecorator :
CacheManager cacheManager = new IgnoreExceptionsCacheManagerDecorator(new DistributedCacheManager());