@Override public Object invoke(MethodInvocation mi, Annotation annotation) throws Throwable { EHCacheKey ehCacheKey = (EHCacheKey) annotation; String key = generatorKey(mi, ehCacheKey); CacheLogger.debug("EHCacheAnnotationProcess", key); EHCacheManager ehCacheManager = new EHCacheManager(ehCacheKey.cacheName()); if (ehCacheManager.keyExists(key)) { if (ehCacheManager.isReload(key)) { Object value = mi.proceed(); if (ehCacheKey.isCacheNull() || value != null) { return push(key, value, ehCacheManager, ehCacheKey); } else { ehCacheManager.delete(key); } return value; } else { return getValue(key, ehCacheManager, ehCacheKey); } } Object value = mi.proceed(); if (ehCacheKey.isCacheNull() || value != null) { push(key, value, ehCacheManager, ehCacheKey); } return value; }
private Object getValue(String key, EHCacheManager ehCacheManager, EHCacheKey ehCacheKey) { long start = System.currentTimeMillis(); try { return ehCacheManager.getSerializableVal(key); } catch (Exception ex) { CacheLogger.error("getValue(" + key + ")", ex); // 反序列失败时,删除Cache ehCacheManager.delete(key); } finally { CacheLogger.writeSlowRequest(start, "EHCacheAnnotationProcess.getValue(" + key + ")"); } return null; }
private Object push( String key, Object value, EHCacheManager ehCacheManager, EHCacheKey ehCacheKey) { long start = System.currentTimeMillis(); try { ehCacheManager.addSerializableVal( key, (Serializable) value, ehCacheKey.isReload(), ehCacheKey.expire()); } catch (Exception ex) { CacheLogger.error("push(" + key + "," + value + ")", ex); } finally { CacheLogger.writeSlowRequest( start, "EHCacheAnnotationProcess.push(" + key + "," + value + ")"); } return value; }