SpringBoot integrates Redis database to realize cache management
- 2021-09-20 20:30:53
- OfStack
2. Spring 2.0 Integrated Redis
1, Core Dependency 2, Configuration File 3, Simple Test Case 4, Custom Serialization Configuration 5, Serialization Test 3. Source Code Address
1. Introduction to Redis
In addition to providing excellent automation support for commonly used relational databases, Spring Boot provides automatic configuration support for many NoSQL databases, including Redis, MongoDB and Elasticsearch. After these cases are sorted out, Git will be uploaded one after another.
SpringBoot2 version supports more and more components. The support for Redis not only extends API, but also replaces the dependence of the underlying Jedis and replaces it with Lettuce.
This case requires a local installation of an Redis database.
2. Spring 2.0 Integrated Redis
1. Core dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. Configuration file
# Port
server:
port: 8008
spring:
application:
# Application name
name: node08-boot-redis
# redis Configure
redis:
host: 127.0.0.1
# Timeout connection
timeout: 1000ms
jedis:
pool:
# Maximum number of connected database connections , Set 0 For there is no limit
max-active: 8
# Maximum number of waiting connections , Set 0 For there is no limit
max-idle: 8
# Maximum connection establishment wait time. If you exceed this time, you will receive an exception. Set to -1 Indicates unlimited.
max-wait: -1ms
# Minimum number of waiting connections , Set 0 For there is no limit
min-idle: 0
In this way, the environment of Redis is successfully configured, and the encapsulated API can be used directly.
3. Simple test cases
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@RestController
public class RedisController {
@Resource
private StringRedisTemplate stringRedisTemplate ;
@RequestMapping("/setGet")
public String setGet (){
stringRedisTemplate.opsForValue().set("cicada","smile");
return stringRedisTemplate.opsForValue().get("cicada") ;
}
@Resource
private RedisTemplate redisTemplate ;
/**
* Settings Key Validity period of 10 Seconds
*/
@RequestMapping("/setKeyTime")
public String setKeyTime (){
redisTemplate.opsForValue().set("timeKey","timeValue",10, TimeUnit.SECONDS);
return "success" ;
}
@RequestMapping("/getTimeKey")
public String getTimeKey (){
// Here Key After expiration, a string is returned 'null'
return String.valueOf(redisTemplate.opsForValue().get("timeKey")) ;
}
}
4. Customize serialization configuration
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
/**
* Redis Configure
*/
@Configuration
public class RedisConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class) ;
/**
* Serialization configuration
*/
@Bean
public RedisTemplate<String, Serializable> redisTemplate
(LettuceConnectionFactory redisConnectionFactory) {
LOGGER.info("RedisConfig == >> redisTemplate ");
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
5. Serialization testing
import com.boot.redis.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@RestController
public class SerializeController {
@Resource
private RedisTemplate redisTemplate ;
@RequestMapping("/setUser")
public String setUser (){
User user = new User() ;
user.setName("cicada");
user.setAge(22);
List<String> list = new ArrayList<>() ;
list.add(" Primary school ");
list.add(" Junior high school ");
list.add(" High school ");
list.add(" University ");
user.setEducation(list);
redisTemplate.opsForValue().set("userInfo",user);
return "success" ;
}
@RequestMapping("/getUser")
public User getUser (){
return (User)redisTemplate.opsForValue().get("userInfo") ;
}
}
3. Source code address
GitHub Address: Cicada 1 Smile
https://github.com/cicadasmile/spring-boot-base
Code Cloud Address: Cicada 1 Smile
https://gitee.com/cicadasmile/spring-boot-base
The above is SpringBoot integration Redis database, cache management details, more about SpringBoot integration Redis information please pay attention to other related articles on this site!