使用spring aop对web 应用数据进行memcached缓存
继续 上一章节**[Memcached 入门-介绍-使用-优化 ]**
http://leiwuluan.iteye.com/blog/1173402
一、 下面主要 实现spring aop 对web 应用数据 进行缓存存取。
说到aop 大家就会想到使用spring 的aop进行切面进行对web Action 的方法进行控制。
下图是实现的流程:

####
这边用到spring的两个类,简单介绍:
1、1 org.springframework.aop.framework.ProxyFactoryBean
spring 这个类可以代理目标类的所有方法 。
<bean id="surroundAdvice" class="com.memCached.service.aop.SurroundAdvice">
<property name="memCachedClient" ref="memCachedClient" />
</bean>
<bean id="proxyFactory" abstract="true" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames">
<idref local="surroundAdvice" />
</property>
<property name="proxyTargetClass" value="true" />
</bean>
<bean id="studentRestService" parent="proxyFactory">
<property name="target" ref="_studentRestService" />
</bean>
<bean id="_studentRestService" class="com.student.rest.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
</bean>
<bean id="studentDao" class="com.student.dao.impl.StudentDaoImpl" />
_studentRestService里面的所有方法将会被 surroundAdvice 给拦截。
spring 代理可以看看这位仁兄的:http://ajava.org/readbook/open/springrmdjt/15705.html
surroundAdvice
implements MethodInterceptor 实现这个接口 重写invoke 方法
在这个方法里进行控制 如下,
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
String fname = arg0.getMethod().getName();
String key = getKey(fname, arg0.getArguments());//方法名和参数组合成key
StringBuffer sb = new StringBuffer();
Object resultObj = null;
if (memCachedClient != null) {
if(MemcachedConf.CACHE_FLAG==MemcacheFlag.CLOSE_CACHE){
memCachedClient.delete(key);
sb.append(",不使用缓存");
resultObj = arg0.proceed();
}else{
resultObj = memCachedClient.get(key);
if(resultObj==null){
resultObj = arg0.proceed();
ResultDto s = (ResultDto)resultObj;
if("success".equals(s.returncode)){
sb.append(",数据存入缓存");
memCachedClient.set(key, s, new Date(1000*60*60));
}
}else{
sb.append(",缓存数据");
}
}
}else{
resultObj = arg0.proceed();
}
return resultObj;
}
二、 一步一步 开始,从安装memcached 到web 搭建,运行,测试(有缓存与没有缓存的访问时间,测试)
2、1 安装 ,以windows下为例吧,linux下也是一样的简单。
http://leiwuluan.iteye.com/blog/1172798
2、2 web 搭建 ,数据库 :mysql,服务用 :rest,数据库操作 :Jdbc连接池
下面工程,不多说。
注意:ITeye最多10MB 只好把里面jar去了。
可以到官网下jar 包.
参考:
http://leiwuluan.iteye.com/blog/1173402
下个cxf:官方地址:http://cxf.apache.org/download.html
2、3 测试结果显示
无缓存处理时间:

---------------------------------------------------------------------------------------------
缓存数据处理时间:

待续...........................
如何处理脏数据。。。