Correct use of springboot~redis

Redis implements the caching of data. In the project, some dictionary data, session data and temporary data will be stored in redis. In springboot, redis is also supported. In general, it is risky for multiple threads to use a redis implementation together. However, each thread is too wasteful to control the number of threads, which is very dangerous So there are some redis thread pool components. Let's talk about two main components.

  • jedis thread pool is mainly that each instance has its own thread, which can be obtained from the pool it establishes
  • Lettuce lettuce is a thread pool tool launched by apache. Its redis instance can be shared and accessed by multiple threads, which improves resource utilization

redis serialization configuration

Generally speaking, redis key uses string serialization; redis value uses json serialization. json is small in size and high in readability, so there is no need to implement the serializer interface.

/**
 * Configuration of redis
 */
@Configuration
public class RedisConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * redis Rewrite RedisTemplate
     */
    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //When serializing, all non constant fields are allowed to output types, that is, redis serialized with types
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // Serialization of redis key
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);

        // Serialization of redis value
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}

In the above code, om.enableDefaultTyping ( ObjectMapper.DefaultTyping.NON_ The json string generated by final) is of type, so that it can be directly inferred from the type when deserializing.

[
  "com.lind.basic.entity.Token",
  {
    "credentials": "ok",
    "region": "hello",
    "bucket": null
  }
]

Take a look at the added and read code

 @GetMapping("set")
    public String set() throws JsonProcessingException {
        Token token = Token.builder()
                .credentials("ok")
                .region("hello")
                .build();
        redisTemplate.opsForValue().set("test:user", token);//redisTemplate helps us serialize
        redisTemplate.opsForHash().put("author", "zzl", token);
        return "OK";
    }

    @GetMapping("get")
    public Token get() throws IOException {
        return (Token) redisTemplate.opsForValue().get("test:user");
    }

Note: for entity class Token, it should have a parameter free constructor, which is required for deserialization.

Tags: Java Redis JSON Session SpringBoot

Posted on Thu, 28 May 2020 10:54:05 -0400 by bodzan