|
好久没更新blog了,在新公司打拼了两个月,每天都从早忙到晚,学到了很多东西,但是没有时间来更新blog了。。。。
下面开始讲解这次的主题
公司老大让我研究 ocs 就是阿里云的 开放缓存服务 点击打开链接
其实就是一个memcache的服务
memchech 就是用内存来存放 key -value 在一些情况下就不必频繁的访问 数据库了(其实就是个map)
如何在常用的Dao中方便的使用这个map呢,我首先想到了 aop,然后又想到了自定义注解,那么就开始干吧。。。。
aop动态代理要使用的jar包如下
aspectjrt-1.6.12.jar(jdk7.0 需要这么高的版本,否则报错)
aspectjweaver-1.6.12.jar
cglib-nodep-2.2.jar
com.springsource.org.aopalliance-1.0.0.jar
和其他的spring包请自行搜索
spring加上这一行 开启自动代理:
<aop:aspectj-autoproxy/>
我的spring配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 默认扫描的包路径 -->
<context:component-scan base-package="com.test" />
<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<aop:aspectj-autoproxy/>
</beans>
首先模拟一个 memCache 我写了一个工具类
package com.test.action;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
public class OcsUtil {
private static Map<String, Object> memMap=new HashMap<String, Object>();
//模拟
public static void add(String key,Object val) throws IOException, InterruptedException, ExecutionException{
memMap.put(key, val);
}
//模拟
public static Object get(String key) throws IOException{
return memMap.get(key);
}
}
这个就暂时代替memCached吧。
dao层:
package com.test.dao;
import org.springframework.stereotype.Repository;
import com.test.aop.MemCache;
@Repository
public class TestDao {
@MemCache
public Object selectKeyValue(String arg1,Integer arg2){
return "12345";
}
}
两个参数随便写的,这样用来模拟真实情况下的多个查询参数
方法名上的@MemCache 注解 就是我自己定义的注解,当拥有这个注解的方法在调用时首先会在memCache中去找,这样就提高了性能
自定义注解如下:
package com.test.aop;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
@Target({METHOD})
@Retention(RetentionPolicy.CLASS)
public @interface MemCache {
}
我们要做的就是用aop拦截 拥有@MemCache注解的方法,然后处理它。下面是aop拦截器
package com.test.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
import com.test.action.OcsUtil;
@Component
@Aspect
public class MemCacheInterceptor {
private Gson gson=new Gson();
@Pointcut("execution(* com.test.dao.TestDao.select*(..))&& @annotation(com.test.aop.MemCache)")
private void theMethod(){}//定义一个切入点
@Around("theMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
String key=pjp.getSignature().toString();
key+=gson.toJson(pjp.getArgs());
Object memValue=OcsUtil.get(key);
if(memValue!=null){
System.out.println("memcache获取到");
return memValue;
}
Object object = pjp.proceed();//执行该方法
OcsUtil.add(key, object+" cache");
System.out.println("db获取");
return object;
}
}
*1为了测试我在memCache中放的时候追加了一个字符串 “ cache”
这个拦截器 使用 “方法的签名” 和 “参数转换成的json字符串” 作为 memCache的key
就像这个样子 Object com.test.dao.TestDao.selectKeyValue(String,Integer)["1",2]
这样就可以保证memCache的存取与dao的操作对应了
这样就大功告成了。
版权声明:本文为博主原创文章,未经博主允许不得转载。 |
|
|