📄 methodcacheafteradvice.java
字号:
package sample.auth.interceptor;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Iterator;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
public class MethodCacheAfterAdvice implements AfterReturningAdvice,InitializingBean {
private static final Log logger = LogFactory.getLog(MethodCacheAfterAdvice.class);
private Cache cache;
private CacheManager cacheManager;
// cacheManager = CacheManager.create();
public void setCache(Cache cache) {
this.cache = cache;
}
public MethodCacheAfterAdvice() {
super();
}
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
for(Iterator itr = cache.getKeys().iterator(); itr.hasNext();){
Serializable key = (Serializable)itr.next();
if(key.toString().indexOf(method.getDeclaringClass().getName()) != -1){
cache.remove(key);
logger.info("remove cache after db change"+key);
}
}
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "there must be a cache");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -