SpringBoot项目使用redis非常简单,pom里面引入redis的场景启动器,在启动类上加@EnableCaching注解,项目启动会自动匹配上redis,这样项目中就可以愉快地使用了,
使用方法:要么使用@Cacheable一类的注解自动缓存,要么使用RedisTemplate手动缓存。
(前提是你的本机或者是远程主机要先搭好redis环境)
虽然SpringBoot好用,但这里也有好多坑,SpringBoot和MySQL一样,易学难精,阳哥说的对,练武不练功,到老一场空。
下面,我将详细记录整合步骤
1.创建一个SpringBoot项目
pom文件中引入cache和redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.新建cache数据库,创建员工表employee
写好DAO,service,controller,配置文件application.yml中配好数据源和redis地址
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3305/cache
username: root
password: 123456
redis:
host: 127.0.0.1
port: 6379
password: hehuiredis177
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations:
- classpath:mybatis/mapper
@Configuration
public class MyRedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
template.setDefaultSerializer(serializer);
return template;
}
}
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Object.class);
template.setDefaultSerializer(serializer);
只多了这两行代码,其它什么都没变。
为什么要用Jackson2JsonRedisSerializer呢?
因为Jackson2JsonRedisSerializer实现了RedisSerializer接口
9.再访问 http://localhost:8080/emp/1
观察redis
key是我们要的key,value是我们要的value
再刷新页面
不会吧!!!
去缓存找,找到了,反序列化失败…
转换异常,看来不能用Jackson2JsonRedisSerializer,换成GenericJackson2JsonRedisSerializer
换了之后,刷新页面
报错信息变了,这是不能读取json,现在redis里的json是Jackson2JsonRedisSerializer序列化的,
GenericJackson2JsonRedisSerializer当然不认识咯,清了redis再试
好了。
10.试试查所有 http://localhost:8080/list
报错!!!
SimpleKey and no properties discovered 没有key 什么情况?
于是继续读源码,看key是咋生成的
public static Object generateKey(Object... params) {
if (params.length == 0) {
return SimpleKey.EMPTY;
}
if (params.length == 1) {
Object param = params[0];
if (param != null && !param.getClass().isArray()) {
return param;
}
}
return new SimpleKey(params);
}
是根据参数来的,我查询所有,是没有参数的,这样key就为空
if (params.length == 0) {
return SimpleKey.EMPTY;
}
这怎么行,开发中,不传参的方法多的很,这样生成key肯定不行撒
于是重写key的生成策略,用【类名+方法名+参数名】这样就可以保证key不为空
package com.hehui.config;
import java.lang.reflect.Method;
import java.net.UnknownHostException;
import java.util.Arrays;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
@Configuration
public class MyRedisConfig extends CachingConfigurerSupport{
@Bean
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
template.setConnectionFactory(redisConnectionFactory);
// Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
template.setDefaultSerializer(serializer);
return template;
}
@Bean
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
public Object generate(Object target, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName()).append(".").append(method.getName()).append(Arrays.toString(objects));
return sb.toString();
}
};
}
}
extends CachingConfigurerSupport并重写keyGenerator方法
测试通过
redis
json中还带有@Class相当好。
11.好啦,相当曲折地把redis整合并自定义配置到SpringBoot中。
源码github地址:https://github.com/hehuihh/springboot_redis
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。