手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

利用Spring AOP 更新Memcached 缓存策略的实现

[日期:2012-03-13] 来源:Linux社区  作者:ajun_studio [字体:大 中 小]
spring AOP代码 基于注解
  1. package com.woaika.loan.front.common.aop;  
  2.   
  3. import Java.lang.reflect.Method;  
  4. import java.util.Date;  
  5. import java.util.List;  
  6.   
  7. import javax.annotation.Resource;  
  8.   
  9. import org.aspectj.lang.ProceedingJoinPoint;  
  10. import org.aspectj.lang.Signature;  
  11. import org.aspectj.lang.annotation.Around;  
  12. import org.aspectj.lang.annotation.Aspect;  
  13. import org.aspectj.lang.annotation.Pointcut;  
  14. import org.aspectj.lang.reflect.MethodSignature;  
  15. import org.springframework.stereotype.Component;  
  16.   
  17. import com.woaika.loan.commons.annotation.Cache;  
  18. import com.woaika.loan.commons.annotation.Flush;  
  19. import com.woaika.loan.commons.cache.Memcache;  
  20. import com.woaika.loan.po.CacheLog;  
  21. import com.woaika.loan.service.log.ICacheLogService;  
  22.   
  23. /** 
  24.  * 拦截缓存 
  25.  * @author ajun 
  26.  * @email zhaojun2066@gmail.com 
  27.  * @blog http://blog.csdn.net/ajun_studio 
  28.  * 2012-3-12 上午10:51:58 
  29.  */  
  30. @Component  
  31. @Aspect  
  32. public class CacheAop {  
  33.   
  34.     private ICacheLogService cacheLogService;  
  35.       
  36.     @Resource(name="cacheLogService")  
  37.     public void setCacheLogService(ICacheLogService cacheLogService) {  
  38.         this.cacheLogService = cacheLogService;  
  39.     }  
  40.   
  41.       
  42.     //定义切面   
  43.     @Pointcut("execution(* com.woaika.loan.service..*.*(..))")  
  44.     public void cachedPointcut() {  
  45.   
  46.     }  
  47.   
  48.     @Around("cachedPointcut()")  
  49.     public Object doAround(ProceedingJoinPoint call){  
  50.          Object result = null;  
  51.          Method[] methods = call.getTarget().getClass().getDeclaredMethods();    
  52.          Signature signature = call.getSignature();  
  53.          MethodSignature methodSignature = (MethodSignature) signature;    
  54.          Method method = methodSignature.getMethod();  
  55.            
  56.          for(Method m:methods){//循环方法,找匹配的方法进行执行   
  57.              if(m.getName().equals(method.getName())){  
  58.                  if(m.isAnnotationPresent(Cache.class)){  
  59.                      Cache cache = m.getAnnotation(Cache.class);  
  60.                      if(cache!=null){  
  61.                             String tempKey = this.getKey(method, call.getArgs());  
  62.                             String prefix = cache.prefix();  
  63.                             String key = prefix+"_"+tempKey;  
  64.                             result =Memcache.get(key);  
  65.                             if(null == result){  
  66.                                 try {  
  67.                                     result = call.proceed();  
  68.                                     long expiration = cache.expiration();//1000*60*60*2==2小时过期   
  69.                                     Date d=new Date();  
  70.                                     d=new Date(d.getTime()+expiration);  
  71.                                     Memcache.set(key, result, d);  
  72.                                     //将key存入数据库   
  73.                                     CacheLog log = new CacheLog();  
  74.                                     log.setPrefix(prefix);  
  75.                                     log.setCacheKey(key);  
  76.                                     this.cacheLogService.add(log);  
  77.                                 } catch (Throwable e) {  
  78.                                     e.printStackTrace();  
  79.                                 }  
  80.                             }  
  81.                               
  82.                         }  
  83.                 } else  if(method.isAnnotationPresent(Flush.class)){  
  84.                      Flush flush = method.getAnnotation(Flush.class);  
  85.                      if(flush!=null){  
  86.                             String prefix = flush.prefix();  
  87.                             List<CacheLog>  logs= cacheLogService.findListByPrefix(prefix);  
  88.                              if(logs!=null && !logs.isEmpty()){  
  89.                                  //删除数据库   
  90.                                 int rows =  cacheLogService.deleteByPrefix(prefix);  
  91.                                 if(rows>0){  
  92.                                     for(CacheLog log :logs){  
  93.                                         if(log!=null){  
  94.                                             String key = log.getCacheKey();  
  95.                                             Memcache.delete(key);//删除缓存   
  96.                                         }  
  97.                                     }  
  98.                                 }  
  99.                              }  
  100.                         }  
  101.                  }else{  
  102.                      try {  
  103.                          result = call.proceed();  
  104.                         } catch (Throwable e) {  
  105.                             e.printStackTrace();  
  106.                         }  
  107.                  }  
  108.                  break;  
  109.              }  
  110.               
  111.         }  
  112.           
  113.           
  114.           
  115.         return result;  
  116.     }  
  117.       
  118.     /** 
  119.      * 组装key值 
  120.      * @param method 
  121.      * @param args 
  122.      * @return 
  123.      */  
  124.    private String getKey(Method method, Object [] args){  
  125.         StringBuffer sb = new StringBuffer();   
  126.         String methodName = method.getName();  
  127.         sb.append(methodName);  
  128.         if(args!=null && args.length>0){  
  129.               
  130.             for(Object arg:args){  
  131.                 sb.append(arg);  
  132.             }  
  133.         }  
  134.           
  135.         return sb.toString();  
  136.          
  137.    }  
  138. }  
service层方法添加注解,但是里面代码没有添加任何memcache客户端的代码,达到降低耦合性:
  1.  @Cache(prefix=CacheConstant.ANLI,expiration=1000*60*60*10)  
  2.  @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly=true)  
  3.     public QueryResult findAll(Integer firstIndex, Integer pageSize) {  
  4.         Map condition = new HashMap();  
  5.         return loanCaseDao.findByCondition(condition, LoanCase.class, firstIndex, pageSize);  
  6.     }  
以上是主要代码的实现思路,希望对同志们的有所帮助,关于spring aop 自行google下就知道了linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款