分享

使用spring aop对web 应用数据进行memcached缓存

 Tom.Lin 2012-07-10

继续 上一章节[Memcached 入门-介绍-使用-优化 ]

 

http://leiwuluan./blog/1173402


一、 下面主要 实现spring aop 对web 应用数据 进行缓存存取。

 

说到aop 大家就会想到使用spring 的aop进行切面进行对web Action 的方法进行控制。

下图是实现的流程:

 

 


####

这边用到spring的两个类,简单介绍:

1、1 org.springframework.aop.framework.ProxyFactoryBean

 

spring 这个类可以代理目标类的所有方法

 

 

Xml代码  收藏代码
  1. <strong>  
  2. </strong>  
  3.   
  4. <bean id="surroundAdvice" class="com.memCached.service.aop.SurroundAdvice">  
  5.   
  6.     <property name="memCachedClient" ref="memCachedClient" />  
  7.   
  8. </bean>  
  9.   
  10.   
  11.   
  12. <bean id="proxyFactory" abstract="true" class="org.springframework.aop.framework.ProxyFactoryBean">  
  13.   
  14.     <property name="interceptorNames">  
  15.   
  16.         <idref local="surroundAdvice" />  
  17.   
  18.     </property>  
  19.   
  20.     <property name="proxyTargetClass" value="true" />  
  21.   
  22. </bean>  
  23.   
  24.   
  25.   
  26. <bean id="studentRestService" parent="proxyFactory">  
  27.   
  28.     <property name="target" ref="_studentRestService" />  
  29.   
  30. </bean>  
  31.   
  32.   
  33.   
  34. <bean id="_studentRestService" class="com.student.rest.service.impl.StudentServiceImpl">  
  35.   
  36.     <property name="studentDao" ref="studentDao" />  
  37.   
  38. </bean>  
  39.   
  40. <bean id="studentDao" class="com.student.dao.impl.StudentDaoImpl" /><strong>  
  41. </strong>  
  42.   
  43.   
  44.   
  45.   
  46. _studentRestService里面的所有方法将会被 surroundAdvice 给拦截。  

 

 

spring 代理可以看看这位仁兄的:http:///readbook/open/springrmdjt/15705.html

 

Xml代码  收藏代码
  1. <strong><span><strong><span style="color: #339966;">surroundAdvice </span>  
  2.   
  3.   
  4.   
  5. </strong>  
  6.   
  7.   
  8.   
  9. </span>  
  10.   
  11.   
  12.   
  13. </strong>  
  14.   
  15.   
  16.   
  17. implements MethodInterceptor 实现这个接口 重写invoke 方法  

 

在这个方法里进行控制 如下,

 

Java代码  收藏代码
  1. @Override  
  2.     public Object invoke(MethodInvocation arg0) throws Throwable {  
  3.   
  4.         String fname = arg0.getMethod().getName();  
  5.         String key = getKey(fname, arg0.getArguments());//方法名和参数组合成key  
  6.         StringBuffer sb = new StringBuffer();  
  7.         Object resultObj = null;  
  8.           
  9.         if (memCachedClient != null) {  
  10.             if(MemcachedConf.CACHE_FLAG==MemcacheFlag.CLOSE_CACHE){  
  11.                 memCachedClient.delete(key);  
  12.                 sb.append(",不使用缓存");  
  13.                 resultObj = arg0.proceed();  
  14.             }else{  
  15.                 resultObj = memCachedClient.get(key);  
  16.                 if(resultObj==null){  
  17.                     resultObj = arg0.proceed();  
  18.                     ResultDto s = (ResultDto)resultObj;  
  19.                     if("success".equals(s.returncode)){  
  20.                         sb.append(",数据存入缓存");  
  21.                         memCachedClient.set(key, s, new Date(1000*60*60));  
  22.                     }  
  23.                 }else{  
  24.                     sb.append(",缓存数据");  
  25.                 }  
  26.             }  
  27.         }else{  
  28.             resultObj = arg0.proceed();  
  29.         }  
  30.         return resultObj;  
  31.     }  
 

 

二、 一步一步 开始,从安装memcached 到web 搭建,运行,测试(有缓存与没有缓存的访问时间,测试)

 

2、1 安装 ,以windows下为例吧,linux下也是一样的简单。

        http://leiwuluan./blog/1172798     

 

 

 

2、2 web 搭建 ,数据库 :mysql,服务用 :rest,数据库操作 :Jdbc连接池

        下面工程,不多说。

       注意:ITeye最多10MB  只好把里面jar去了。

      可以到官网下jar  包.

 

     参考:

     http://leiwuluan./blog/1173402

      下个cxf:官方地址:http://cxf./download.html

 

2、3 测试结果显示

 

无缓存处理时间:


 

---------------------------------------------------------------------------------------------

 

缓存数据处理时间:


 

待续...........................

 

 

 

 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多