directory
The background,
Two, operation steps
1. Jar dependency import
2. Configure management
3. Configuration file
4. Specific use
Third, summary
The background,
Recently in writing springBoot series related things, Redis in the current Internet development is almost a round the topic, the current management system is also used to do. The old usage used to be that everything was wrapped by itself, and Spring was just a container. With the improvement of the whole spring family’s bucket technology and current public usage, the old usage felt a bit outdated and was sometimes ridiculed. But whether the performance piece can hold up will be left to the future. Today we will take a look at how to integrate and explain the basic usage habits.
Two, operation steps
1. Jar dependency import
<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <! Clients </groupId> <artifactId>jedis</artifactId>3.3. 0</version> </dependency> <! Commons -pool2</artifactId> <version> Commons -pool2</artifactId>2.9. 0</version>
</dependency>
Copy the code
Conclusion:
- Spring-boot-starter-data-redis introduces the lettuce client by default, and uses the Jedis client here, so it shields the lettuce dependencies
- Connection pools use pool2, springBoot integrates connection pool Settings and does not need to initialize the connection pool configuration as before. Note that the connection pool properties take effect when configured in the configuration file.
2. Configure management
@Configuration
public class RedisConfiguration {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
/ / use Jackson2JsonRedisSerialize replace the default jdkSerializeable serialization
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
// Set the serialization rules for String value and key
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// Set the serialization rules for hash values and keys
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
returnredisTemplate; }}Copy the code
Conclusion:
- First of all, I have not enabled annotations. If you need them, you can enable @enablecaching.
- Personally, I am not used to complete annotation. In the actual development process, I need to debug the process of Redis. The feeling of annotation is not good, or I recommend myself to write more.
- For serialization, this is just a demonstration with JSON, but other approaches are supported.
3. Configuration file
Host =localhost #Redis server connection port spring.redis. Port =6379#Redis database index (default0) spring. Redis. Database =0
spring.redis.ssl=false# # the following as the connection pool properties, if load connection pool configuration, if there is no configuration is the singleton pattern # connection pool maximum number of connections (use a negative value indicates no limit) spring. Redis. Jedis. Pool. The Max - active =50# connection pool in the largest free connection spring. Redis. Jedis. Pool. The Max - idle =20# connection pool minimum idle connections in the spring. The redis. Jedis. Pool. Min - idle =2# connection pool biggest jam waiting time (use a negative value indicates no limit) (milliseconds) spring. Redis. Jedis. Pool. The Max - wait =3000# idle link detects thread detection cycle. If it is negative, the detection thread is not running. (in milliseconds, the default value is - layer) spring. Redis. Jedis. Pool. The time between -- eviction - runs =6000# connection timeout (ms) spring.redis. Timeout =5000
Copy the code
Conclusion:
- The above configuration is a single-instance connection pool version
- A later addition to the cluster edition
- Again, connection pool information takes effect with or without a declaration
- The connection pool parameter above is not an optimal setting, just to illustrate the point. Later open a special topic to make special introduction
4. Specific use
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
RedisTemplate redisTemplate;
@GetMapping(value = {"/haha"})
public String getUser(a){
User user= new User();
user.setName("name");
user.setPassword("password");
// Store objects
redisTemplate.opsForValue("bbb",user);
// Select * from *; // select *;
user= (User) redisTemplate.opsForValue().get("bbb");
log.info(user.toString());
return "hahah"; }}Copy the code
- Springboot provides two templates, RedisTemplate
,object>- With RedisTemplate
,object>- The general usage is that to encapsulate a RedisUtil utility class, several transformations are required (as above). Here you can see that there is no convenience in using Jedis directly. See personal habit!
Third, summary
- There are a lot of solutions for Springboot to integrate Redis. As for which one is the best, I think it is the best one for me.
- The advantage of using Springboot integration is that it is easy to write, but the disadvantage is that you can’t see anything.