鐢變簬緋葷粺闇姹傞渶
瑕佸鍚勪釜鎺ュ彛榪涜key-value緙撳瓨(浠ュ弬鏁頒負(fù)key,榪斿洖鐨勫璞′負(fù)value),褰撶劧瀵逛簬榪欑鎯呭喌棣栧厛鑰冭檻鍒扮殑鏄嬌鐢╝op,鍓嶆鏃墮棿鐪嬭繃
aspectj鐨勪竴浜涗粙緇?鍊熸鏈轟細(xì)姝eソ鍔犱互搴旂敤鍜屼綋浼?xì)涓涓?aspectj鏄疉OP鏈鏃╂垚鐔熺殑java瀹炵幇,瀹冪◢寰墿灞曚簡(jiǎn)涓涓媕ava璇█,澧炲姞浜?jiǎn)涓浜?
keyword絳?鍏蜂綋鐨刟spectj鐨勫熀鏈娉曡[ur=http://today.java.net/pub/a/today/2003/12
/26/ch3AspectJSyntaxBasics.html]榪欓噷[/url],榪涜緙撳瓨鐨勬鏋朵嬌鐢ㄨ緝鎴愮啛鐨別hcache.
涓嬮潰寮濮嬭繘琛岄厤緗?br />
棣栧厛鏄痚hcache鐨勯厤緗枃浠?br />
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="/home/workspace/gzshine/trunk/ehcache"/>
<cache name="DEFAULT_CACHE"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="true"/>
</ehcache>
榪欎釜鐨凞EFAULT_CACHE鏄粯璁ら厤緗?鏈澶х殑緙撳瓨鏁頒負(fù)10000,鏃墮棿涓轟竴涓皬鏃?br />
鎺ヤ笅鏉ョ殑鏄痵pring涓嬬殑閰嶇疆
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- ############## aspectj 4 ehcache ############# -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id = "methodCacheAspectJ" class="com.***.shine.aspectj.MethodCacheAspectJ" >
<property name="cache">
<ref local="methodCache" />
</property>
</bean>
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<!-- 瀹氫箟ehCache鐨勫伐鍘傦紝騫惰緗墍浣跨敤鐨凜ache name -->
<bean id="methodCache"
class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager" />
</property>
<property name="cacheName">
<value>DEFAULT_CACHE</value>
</property>
</bean>
<aop:aspectj-autoproxy proxy-target-class="true"/>
鏄負(fù)aspectj鍦ㄦ墍鏈塩lass涓嬪紑鍚嚜鍔ㄥ姩鎬佷唬鐞?br />
<bean id="cacheManager">鎸囧畾鍒氬垰鐨別hcache閰嶇疆鏂囦歡
鎺ヤ笅鏉ョ紪鍐欎竴涓嚜瀹氫箟鐨刟nnotation
package com.***.shine.cache;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodCache {
int second() default 0;
}
<bean id = "methodCacheAspectJ">鏄竴涓猘spectj榪涜Pointcuts鍜孉dvice鐨勭被闇娉ㄥ叆methodCache
package com.***.shine.aspectj;
@Aspect
public class MethodCacheAspectJ {
Log logger = LogFactory.getLog(MethodCacheAspectJ.class);
private Cache cache;
/**
* 璁劇疆緙撳瓨鍚?nbsp;
*/
public void setCache(Cache cache) {
this.cache = cache;
}
@Pointcut("@annotation(com.***.shine.cache.MethodCache)")
public void methodCachePointcut(){
}
@Around("methodCachePointcut()")
public Object methodCacheHold(ProceedingJoinPoint joinPoint) throws Throwable{
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Object result = null;
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
if (element == null) {
try{
result = joinPoint.proceed();
}catch(Exception e){
logger.info(e);
}
if(result!=null){
try{
element = new Element(cacheKey, (Serializable) result);
Class targetClass = Class.forName(targetName);
Method[] method = targetClass.getMethods();
int second = 0;
for(Method m:method){
if (m.getName().equals(methodName)) {
Class[] tmpCs = m.getParameterTypes();
if(tmpCs.length==arguments.length){
MethodCache methodCache = m.getAnnotation(MethodCache.class);
second = methodCache.second();
break;
}
}
}
if(second>0){ // annotation娌℃湁璁緎econd鍊煎垯浣跨敤ehcache.xml涓嚜瀹氫箟鍊?nbsp;
element.setTimeToIdle(second);
element.setTimeToLive(second);
}
cache.put(element);
}catch(Exception e){
logger.info("!!!!!!!!!"+cacheKey+"!!!!!!!!!鏈兘鎵ц鏂規(guī)硶緙撳瓨"+e);
}
}
}
return element.getValue();
}
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++) {
if (arguments[i] instanceof Date) {
sb.append(".").append(
DateUtil.datetoString((Date) arguments[i]));
} else {
sb.append(".").append(arguments[i]);
}
}
}
return sb.toString();
}
}
@Pointcut("@annotation(com.netease.shine.cache.MethodCache)")
瀵規(guī)湁搴旂敤com.netease.shine.cache.MethodCache榪涜娉ㄨВ鐨勬柟娉曡繘琛屾í鍒囬潰鎷︽埅
@Around("methodCachePointcut()")
騫跺湪Advice涓鐞嗚繖涓狿ointcut,榪欓噷鐨勭殑Advice浣跨敤鐨勬槸Around(鐜粫閫氱煡)
String cacheKey = getCacheKey(targetName, methodName, arguments);
鎺ヤ笅鏉ヤ嬌鐢ㄧ被鍨?鏂規(guī)硶鍚?鍙傛暟涓簁ey榪涘叆緙撳瓨澶勭悊
Element element = cache.get(cacheKey);
褰撶劧濡傛灉鍦╟ache闃熷垪涓彇寰楅潪null瀵硅薄鍒欑洿鎺ヨ繑鍥炶瀵硅薄
MethodCache methodCache = m.getAnnotation(MethodCache.class);
second = methodCache.second();
鍙栧緱second鐨勫?緙撳瓨鐨勬椂闂?濡傚湪@annotation涓棤閲嶅啓鍙負(fù)int second() default 0)
element.setTimeToIdle(second);
element.setTimeToLive(second);
濡傛灉闈為浂鍒欓噸鏂拌緗紦瀛樻椂闂?br />
@MethodCache(second=300)
public List<Sort> getSort(int type,int parentid){
System.out.println("!!!!!!!!!!!!!娌$紦瀛樺埌");
Row row = new Row();
row.put("type", type);
row.put("parentid", parentid);
return (List<Sort>)gz_Template.queryForList("sort.getSort", row);
}
----------------------------------------
闄堜簬鍠?
Mail: chenyz@corp.netease.com

]]>