基于 自己定义注解 和 aop 实现使用memcache 对数据库的缓存 演示样例

好久没更新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的操作相应了

这样就大功告成了。





原文地址:https://www.cnblogs.com/mfrbuaa/p/4362653.html