目录
一、Spring整合Redis
二、注解式开发
2.9.0
1.7.1.RELEASE redis.clients jedis ${redis.version}
org.springframework.data spring-data-redis ${redis.spring.version}



redis.hostName=192.168.26.128 //ip地址
redis.port=6379 //端口号
redis.password=123456 //密码
redis.timeout=10000
redis.maxIdle=300
redis.maxTotal=1000
redis.maxWaitMillis=1000
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.testOnBorrow=true
redis.testWhileIdle=true
redis.expiration=3600 //缓存有效时间
注意:redis.properties与jdbc.properties在与Spring做整合时会发生冲突;所以引入配置文件的地方要放到applicationContext.xml中
将其修改为外部多文件方式,即可用MySQL也能用Redis
classpath:jdbc.properties classpath:redis.properties
CacheKeyGenerator.java:
package com.zq.ssm.redis;import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.util.ClassUtils;import java.lang.reflect.Array;
import java.lang.reflect.Method;@Slf4j
public class CacheKeyGenerator implements KeyGenerator {// custom cache keypublic static final int NO_PARAM_KEY = 0;public static final int NULL_PARAM_KEY = 53;@Overridepublic Object generate(Object target, Method method, Object... params) {StringBuilder key = new StringBuilder();key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":");if (params.length == 0) {key.append(NO_PARAM_KEY);} else {int count = 0;for (Object param : params) {if (0 != count) {//参数之间用,进行分隔key.append(',');}if (param == null) {key.append(NULL_PARAM_KEY);} else if (ClassUtils.isPrimitiveArray(param.getClass())) {int length = Array.getLength(param);for (int i = 0; i < length; i++) {key.append(Array.get(param, i));key.append(',');}} else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) {key.append(param);} else {//Java一定要重写hashCode和eqaulskey.append(param.hashCode());}count++;}}String finalKey = key.toString();
// IEDA要安装lombok插件log.debug("using cache key={}", finalKey);return finalKey;}
}

首先我们创建一个测试类,并且要加入注释:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
package com.zq.ssm;import com.zq.ssm.biz.ClazzBiz;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class ClazzBizTest {@Autowiredprivate ClazzBiz clazzBiz;@Testpublic void test1(){System.out.println(clazzBiz.selectByPrimaryKey(10));System.out.println(clazzBiz.selectByPrimaryKey(10));}@Testpublic void test2(){clazzBiz.deleteByPrimaryKey(10);}
}
配置在方法或类上,作用:本方法执行后,先去缓存看有没有数据,如果没有,从数据库中查找出来,给缓存中存一份,返回结果, 下次本方法执行,在缓存未过期情况下,先在缓存中查找,有的话直接返回,没有的话从数据库查找
@Cacheable(value = "user-clz",key = "'clz:'+#cid",condition = "#cid > 5")
Clazz selectByPrimaryKey(Integer cid);
value:缓存位置的一段名称,不能为空
key:缓存的key,默认为空,表示使用方法的参数类型及参数值作为key,支持SpEL
condition:触发条件,满足条件就加入缓存,默认为空,表示全部都加入缓存,支持SpEL
那么我们执行测试语句一的话就只会查询一遍数据库,第二遍的数据来源于Redis缓存

类似于更新操作,即每次不管缓存中有没有结果,都从数据库查找结果,并将结果更新到缓存,并返回结果
@CachePut(value = "user-clz-put")
value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个
key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合
condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存
这个就是只是加入缓存,但是我们的数据还是来源自数据库
用来清除用在本方法或者类上的缓存数据(用在哪里清除哪里)
value:缓存位置的一段名称,不能为空
key:缓存的key,默认为空,表示使用方法的参数类型及参数值作为key,支持SpEL
condition:触发条件,满足条件就加入缓存,默认为空,表示全部都加入缓存,支持SpEL
allEntries:true表示清除value中的全部缓存,默认为false
// @CacheEvict(value = "user-clz-put",key = "'clz:'+#cid") 删除指定的缓存数据@CacheEvict(value = "user-clz-put",allEntries = true) // 删除以 user-clz-put开头的 缓存int deleteByPrimaryKey(Integer cid);
第一个注释就是删除指定得缓存,第二个就是删除所有的缓存信息。
(可用于电商或者平常的秒杀活动)