java代碼: |
/* * 創(chuàng)建日期 2005-3-15 * * TODO 要更改此生成的文件的模板,請轉(zhuǎn)至 * 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板 */ package com.cnsi.softer.interceptor.MethodCacheInterceptor; import java.io.Serializable; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; /** * @author Administrator * * TODO 要更改此生成的類型注釋的模板,請轉(zhuǎn)至 * 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板 */ class MethodCacheInterceptor implements MethodInterceptor, InitializingBean { private static final Log logger = LogFactory.getLog(MethodCacheInterceptor.class); private Cache cache; public void setCache(Cache cache){ this.cache = cache; } /** * */ public MethodCacheInterceptor() { super(); // TODO 自動(dòng)生成構(gòu)造函數(shù)存根 } /** * 主方法 * 如果某方法可被緩存就緩存其結(jié)果 * 方法結(jié)果必須是可序列化的(serializable) */ public Object invoke(MethodInvocation invocation) throws Throwable { String targetName = invocation.getThis().getClass().getName(); String methodName = invocation.getMethod().getName(); Object[] arguments = invocation.getArguments(); Object result; logger.debug("在緩存中查找方法返回的對象!"); String cacheKey = getCacheKey(targetName, methodName, arguments); Element element = cache.get(cacheKey); if (element == null) { logger.debug("正在攔截方法!"); result = invocation.proceed(); logger.debug("正在緩存對象!"); element = new Element(cacheKey, (Serializable) result); cache.put(element); } return element.getValue(); } /** *創(chuàng)建一個(gè)緩存對象的標(biāo)識: targetName.methodName.argument0.argument1... */ private String getCacheKey(String targetName, String methodName, Object[] arguments) { StringBuffer sb = new StringBuffer(); sb.append(targetName) .append(".").append(methodName); if ((arguments != null) && (arguments.length != 0)) { for (int i=0; i<arguments.length; i++) { sb.append(".") .append(arguments[i]); } } return sb.toString(); } /* £ǚÇ Javadoc) * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ public void afterPropertiesSet() throws Exception { Assert.notNull(cache, "需要一個(gè)緩存. 使用setCache(Cache)分配一個(gè)."); } } |
java代碼: |
/* * 創(chuàng)建日期 2005-3-15 * * TODO 要更改此生成的文件的模板,請轉(zhuǎn)至 * 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板 */ package com.cnsi.softer.interceptor.MethodCacheInterceptor; import java.lang.reflect.Method; import net.sf.ehcache.Cache; 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; /** * @author Administrator * * TODO 要更改此生成的類型注釋的模板,請轉(zhuǎn)至 * 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板 */ public class MethodCacheAfterAdvice implements AfterReturningAdvice,InitializingBean { private static final Log logger = LogFactory.getLog(MethodCacheAfterAdvice.class); private Cache cache; public void setCache(Cache cache){ this.cache = cache; } /** * */ public MethodCacheAfterAdvice() { super(); } /* (非 Javadoc) * @see org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], java.lang.Object) */ public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { StringBuffer buffer = new StringBuffer(); buffer.append(arg3.getClass().getName()).append(".").append(arg1.getName()); if (arg2 != null&&arg2.length != 0){ for (int i=0; i<arg2.length; i++) { buffer.append(".") .append(arg2[i]); } } cache.remove(buffer); } /* (非 Javadoc) * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ public void afterPropertiesSet() throws Exception { Assert.notNull(cache, "需要一個(gè)緩存. 使用setCache(Cache)分配一個(gè)."); } } |