Recommended article reading
Click me to Read SpringBoot and caching ~ concepts, annotations, Redis integration, distributed locking
Click Me to Read SpringBoot Cache how SpringBoot cache works and how @cacheable runs
Click Me to Read SpringBoot
preface
Color {red} It is better to teach a man to fish than to teach a man to fish than to teach a man to fish, the core is that everyone on the Internet talk about integration steps 1/2/3 listed, does not teach us how to find this method and configuration. \ color {red} everyone online is about integration steps listed 1/2/3, didn’t teach you how to find the method and the configuration, everyone online is about integration steps listed 1/2/3, didn’t teach you how to find the methods and configuration, here the method to share with everyone, \color{red} \color{red} \color{red} \color{red
Integrate Redis cache ~ version 1.5.12
- Set up redis server
2, Introduce POM dependency, how to find this dependency?
https://docs.spring.io/spring-boot/docs/2.1.18RELEASE/reference/HTML/https://docs.spring.io/spring-boot/docs/ instead1.5.12.RELEASE/reference/html/
Copy the code
The method is a little low, but really can not find the 1.5.12 version of the document entry
Find: 13.5. Starters
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Copy the code
- Use the @enablecaching annotation
@SpringBootApplication
@EnableCaching
@MapperScan("csdn.xiaozheng.springbootcachexiaozheng.mapper")
public class SpringBootCacheXiaozhengApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootCacheXiaozhengApplication.class, args); }}Copy the code
- Configure Redis. What configuration does Redis support
Common application properties
Copy the code
Or search
spring.redis.host
Copy the code
# 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:[email protected]: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.
Copy the code
So you know which configurations are supported
spring.redis.host=127.0.0.1
Copy the code
Integration completed, testing
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(a){
Department department = departmentService.get(1);
System.out.println(department.toString());
}
@Test
public void test001(a){
redisTemplate.opsForValue().set("name"."xiaozheng");
Object name = redisTemplate.opsForValue().get("name"); System.out.println(name.toString()); }}Copy the code
Tool view:
Existing serialization issues
From the figure above, we can see that the content xiaozheng saved into Redis becomes \xAC\xED\x00\x05t\x00\x09xiaozheng
@Test
public void test002(a){
Employee employee = employeeService.get(1);
redisTemplate.opsForValue().set("emp-01", employee);
}
Copy the code
By default, it uses the SEQUENTIAL mechanism of the JDK
Custom serialization mechanism
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);
returntemplate; }}Copy the code
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(a){
Department department = departmentService.get(1);
System.out.println(department.toString());
}
@Test
public void test001(a){
redisTemplate.opsForValue().set("name"."xiaozheng");
Object name = redisTemplate.opsForValue().get("name");
System.out.println(name.toString());
}
@Test
public void test002(a){
Employee employee = employeeService.get(1);
redisTemplate.opsForValue().set("emp-01", employee);
}
@Test
public void test004(a){
Employee employee = employeeService.get(1);
empRedisTemplate.opsForValue().set("emp-004", employee); }}Copy the code
conclusion
The more you know, the more you don’t know, I hope it will be helpful to you. Thank you! \color{red} the more you know, the more you don’t know, I hope to help you, thank you! The more you know, the more you don’t know, I hope to help you, thank you!