前言
整合redis缓存 ~ 版本:1.5.12
- 搭建redis服务器
2、引入pom依赖,如何找这个依赖呢?
https://docs.spring.io/spring-boot/docs/2.1.18.RELEASE/reference/html/
改为
https://docs.spring.io/spring-boot/docs/1.5.12.RELEASE/reference/html/
方法有点low,不过是真的找不到1.5.12的版本文档入口呀
找到:13.5. Starters
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 使用@EnableCaching注解
@SpringBootApplication
@EnableCaching
@MapperScan("csdn.xiaozheng.springbootcachexiaozheng.mapper")
public class SpringBootCacheXiaozhengApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootCacheXiaozhengApplication.class, args);
}
}
- 配置redis,redis支持哪些配置呢
Common application properties
或者搜搜
spring.redis.host
# REDIS (RedisProperties)
spring.redis.cluster.max-redirects= # Maximum number of redirects to follow when executing commands across the cluster.
spring.redis.cluster.nodes= # Comma-separated list of "host:port" pairs to bootstrap from.
spring.redis.database=0 # Database index used by the connection factory.
spring.redis.url= # Connection URL, will override host, port and password (user will be ignored), e.g. redis://user:password@example.com:6379
spring.redis.host=localhost # Redis server host.
spring.redis.password= # Login password of the redis server.
spring.redis.ssl=false # Enable SSL support.
spring.redis.pool.max-active=8 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
spring.redis.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.port=6379 # Redis server port.
spring.redis.sentinel.master= # Name of Redis server.
spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs.
spring.redis.timeout=0 # Connection timeout in milliseconds.
这样你就知道支持哪些配置了
spring.redis.host=127.0.0.1
整合完毕,测试
package csdn.xiaozheng.springbootcachexiaozheng;
import csdn.xiaozheng.springbootcachexiaozheng.domain.Department;
import csdn.xiaozheng.springbootcachexiaozheng.service.DepartmentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@EnableCaching
@RunWith(SpringRunner.class)
public class SpringBootCacheXiaozhengApplicationTests{
@Autowired
private DepartmentService departmentService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void test1(){
Department department = departmentService.get(1);
System.out.println(department.toString());
}
@Test
public void test001(){
redisTemplate.opsForValue().set("name", "xiaozheng");
Object name = redisTemplate.opsForValue().get("name");
System.out.println(name.toString());
}
}
工具查看:
存在的序列化问题
从上图我们可以看出,存入redis的内容xiaozheng变成了\xAC\xED\x00\x05t\x00\x09xiaozheng
@Test
public void test002(){
Employee employee = employeeService.get(1);
redisTemplate.opsForValue().set("emp-01", employee);
}
在默认情况下呢,它使用的是jdk的序列化机机制
自定义序列化机制
package csdn.xiaozheng.springbootcachexiaozheng.config;
import csdn.xiaozheng.springbootcachexiaozheng.domain.Department;
import csdn.xiaozheng.springbootcachexiaozheng.domain.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import java.net.UnknownHostException;
@Configuration
public class MyRedisConfig {
@Bean
public RedisTemplate<Object, Employee> empRedisTemplate(
RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> ser = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
template.setDefaultSerializer(ser);
return template;
}
@Bean
public RedisTemplate<Object, Department> deptRedisTemplate(
RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Department> template = new RedisTemplate<Object, Department>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Department> ser = new Jackson2JsonRedisSerializer<Department>(Department.class);
template.setDefaultSerializer(ser);
return template;
}
}
package csdn.xiaozheng.springbootcachexiaozheng;
import csdn.xiaozheng.springbootcachexiaozheng.domain.Department;
import csdn.xiaozheng.springbootcachexiaozheng.domain.Employee;
import csdn.xiaozheng.springbootcachexiaozheng.service.DepartmentService;
import csdn.xiaozheng.springbootcachexiaozheng.service.EmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@EnableCaching
@RunWith(SpringRunner.class)
public class SpringBootCacheXiaozhengApplicationTests{
@Autowired
private DepartmentService departmentService;
@Autowired
private EmployeeService employeeService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate<Object, Employee> empRedisTemplate;
@Test
public void test1(){
Department department = departmentService.get(1);
System.out.println(department.toString());
}
@Test
public void test001(){
redisTemplate.opsForValue().set("name", "xiaozheng");
Object name = redisTemplate.opsForValue().get("name");
System.out.println(name.toString());
}
@Test
public void test002(){
Employee employee = employeeService.get(1);
redisTemplate.opsForValue().set("emp-01", employee);
}
@Test
public void test004(){
Employee employee = employeeService.get(1);
empRedisTemplate.opsForValue().set("emp-004", employee);
}
}
总结
这里并不展开讲redisTemplate如何使用,重点在于整合