memcached它是一个缓存服务器,为的就是减少mysql数据库的压力
原先的流程是 客户-->controller-->service-->dao-->mysql再反馈结果
现在加上memcached 就是,先从memcached里查看有没有对应的缓存数据,如果有就直接从memcached里拿,就不走mysql了,如果没有的话再从mysql数据库拿,然后往memcached里存一份.
要实现这样的逻辑需要使用AOP来实现
配置AOP要对查询的方法进行缓存数据.对增删改的方法进行缓存数据销毁.才能做到数据同步.
把切入点修改成自己项目中的就可以了
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- Memcached 配置 -->
<bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
<constructor-arg>
<value>sockIOPool</value>
</constructor-arg>
</bean>
<!-- Memcached连接池 -->
<bean id="sockIOPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>sockIOPool</value>
</constructor-arg>
<property name="servers">
<list>
<value>192.168.200.149:11211</value>
<!-- <value>192.168.200.150:11211</value>
<value>192.168.200.151:11211</value> -->
</list>
</property>
<property name="weights">
<list>
<value>1</value>
<!-- <value>2</value>
<value>3</value> -->
</list>
</property>
</bean>
<!-- 切面对象 -->
<bean id="cacheInterceptor" class="cn.itcast.common.web.aop.CacheInterceptor">
<property name="expiry" value="4200000"/>
</bean>
<!-- Spring Aop 配置 get* 配置环绕 -->
<aop:config>
<!-- 面 -->
<aop:aspect ref="cacheInterceptor">
<!-- 点 -->
<aop:around method="doAround" pointcut="execution(* cn.itcast.core.service.*.*.get*(..))"/>
<!-- 变更 -->
<aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.update*(..))"/>
<aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.add*(..))"/>
<aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.delete*(..))"/>
</aop:aspect>
</aop:config>
</beans>
编写切面对象
package cn.itcast.common.web.aop;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.springframework.beans.factory.annotation.Autowired;
import com.danga.MemCached.MemCachedClient;
/**
* 缓存Memcached中数据的切面对象
* aroud
* after
* @author lx
*
*/
public class CacheInterceptor {
@Autowired
private MemCachedClient memCachedClient;
//时间 缓存时间 key保存的时间
public static final int TIMEOUT = 360000;//秒
private int expiry = TIMEOUT;
//配置环绕方法
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
//去Memcached中看看有没有我们的数据 包名+ 类名 + 方法名 + 参数(多个)
String cacheKey = getCacheKey(pjp);
System.out.println(cacheKey);
//如果Memcached 连接不上呢
if(memCachedClient.stats().isEmpty()){
System.out.println("Memcached服务器可能不存在或是连接不上");
return pjp.proceed();
}
Object object = memCachedClient.get(cacheKey);
//返回值
if(null == object){
//回Service
Object proceed = pjp.proceed();
//先放到Memcached中一份
memCachedClient.set(cacheKey, proceed,expiry);
}
return memCachedClient.get(cacheKey);
}
//后置由于数据库数据变更 清理get*
public void doAfter(JoinPoint jp){
//包名+ 类名 + 方法名 + 参数(多个) 生成Key
//包名+ 类名
String packageName = jp.getTarget().getClass().getName();
//包名+ 类名 开始的 都清理 下面这个方法是获取所有的key
Map<String, Object> keySet = MemCachedUtil.getKeySet(memCachedClient);
//
Set<Entry<String, Object>> entrySet = keySet.entrySet();
//遍历
for(Entry<String, Object> entry : entrySet){
if(entry.getKey().startsWith(packageName)){
memCachedClient.delete(entry.getKey());
}
}
}
//包名+ 类名 + 方法名 + 参数(多个) 生成Key
public String getCacheKey(ProceedingJoinPoint pjp){
//StringBuiter
StringBuilder key = new StringBuilder();
//包名+ 类名 cn.itcast.core.serice.product.ProductServiceImpl.productList
String packageName = pjp.getTarget().getClass().getName();
key.append(packageName);
// 方法名
String methodName = pjp.getSignature().getName();
key.append(".").append(methodName);
//参数(多个)
Object[] args = pjp.getArgs();
ObjectMapper om = new ObjectMapper();
om.setSerializationInclusion(Inclusion.NON_NULL);
for(Object arg : args){
//流
StringWriter str = new StringWriter();
//对象转Json 写的过程 Json是字符串流
try {
om.writeValue(str, arg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//参数
key.append(".").append(str);
}
return key.toString();
}
public void setExpiry(int expiry) {
this.expiry = expiry;
}
}