17_10_24 Redis 实例
2017-10-24 10:03 小歪1991 阅读(171) 评论(0) 编辑 收藏 举报一,下载Redis,并打开
二,ssm中
poml.xml中:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.2.RELEASE</version>
</dependency>
<!-- redis客户端jar -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
<!-- 字节码操作包,用于动态生成代理类之类的操作,用于序列化和反序列化 -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<!-- aop注解 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
mybatis-spring.xml中:
<!-- redis数据源 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- redis连接池 -->
<bean class="redis.clients.jedis.JedisPool" destroy-method="close">
<constructor-arg name="poolConfig" ref="poolConfig" />
<constructor-arg name="host" value="${redis.host}" />
<constructor-arg name="port" value="${redis.port}" />
</bean>
<!-- Spring-redis连接池管理工厂 -->
<bean
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.pass}"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<bean id="t" class="cm.duu.aop.T">
</bean>
<!-- Spring Aop 配置 get* 配置环绕 -->
<aop:config>
<!-- 面 -->
<aop:aspect ref="t">
<!-- 点 -->
<aop:around method="doAround"
pointcut="execution(* cm.duu.service.*.query*(..))" />
<aop:after method="doafter" pointcut="execution(* cm.duu.service.*.add*(..))"/>
</aop:aspect>
</aop:config>
实体类:
package cm.duu.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import cm.duu.util.SerializeUtil;
import redis.clients.jedis.Jedis;
public class T {
@Autowired
private JedisConnectionFactory jedisConnectionFactory;
// 包名+ 类名 + 方法名 + 参数(多个) 生成Key
public static String getRedisKey(ProceedingJoinPoint pjp) {
StringBuilder key = new StringBuilder();
String packageAndClassName = pjp.getTarget().getClass().getName();
key.append(packageAndClassName);
String methodName = pjp.getSignature().getName();
key.append(".").append(methodName);
Object[] args = pjp.getArgs();
for (Object arg : args) {
key.append(".").append(arg);
}
return key.toString();
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
Jedis jedis = jedisConnectionFactory.getConnection().getNativeConnection();
String queryKey = getRedisKey(pjp);
byte[] queryFromRedis = jedis.get(queryKey.getBytes());
System.out.println("我们认为应该出现map" + queryFromRedis);
if (queryFromRedis != null) {
System.out.println("redis里面有数据");
return SerializeUtil.unserizlize(queryFromRedis);
} else {
System.out.println("redis里面没有有数据!!!!!");
Object queryDataFromMysql = pjp.proceed();
jedis.set(queryKey.getBytes(), SerializeUtil.serialize(queryDataFromMysql));
return queryDataFromMysql;
}
}
public void doafter(){
Jedis jedis = jedisConnectionFactory.getConnection().getNativeConnection();
jedis.flushAll();
}
}
工具类:因为redis在存储或者获取对象的时候,必须转化序列才能转化,如:Map<String,List<Student>>这样比较复杂的情况的存储
package cm.duu.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeUtil {
// 序列化
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream bai = new ByteArrayOutputStream();
ObjectOutputStream obi = new ObjectOutputStream(bai);
obi.writeObject(obj);
byte[] byt = bai.toByteArray();
return byt;
}
// 反序列化
public static Object unserizlize(byte[] byt) throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(byt);
ObjectInputStream oii = new ObjectInputStream(bis);
Object obj = oii.readObject();
return obj;
}
}
Jedis流程
如下: