Redis learning notes 2-custom redisTimple

Redis learning notes 2-custom redisTimple

  1. Why customize?
    Java provides a mechanism for object serialization. In this mechanism, an object can be represented as a byte sequence. The byte sequence includes the data of the object, the information about the type of the object, and the type of data stored in the object. . After the serialized object is written into the file, it can be read from the file and deserialized, that is to say, the type information of the object, the data of the object, and the data type in the object can be used in memory Create a new object in. -from net

    Redis database stores data in the form of key-value pairs. If you want to pass in an object, you must serialize the object. redisTimple provides the default serialization tool: JdkSerializationRedisSerializer, but the keys stored in the redis database will be transferred from the console first. Meaning characters, so you need to customize the implementation of redisTimple

  2. RedisAutoConfiguration method

        @Bean
    @ConditionalOnMissingBean(
        name = {
          
          "redisTemplate"}
    )
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
          
          
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
    
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
          
          
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
    
  3. Implement redisTimple

	@Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    
    
        RedisTemplate<String, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);

        //JSON序列化配置
        Jackson2JsonRedisSerializer Jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL);
        Jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        //String 序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setValueSerializer(Jackson2JsonRedisSerializer);
        template.setHashKeySerializer(Jackson2JsonRedisSerializer);

        template.afterPropertiesSet();

        return template;
    }

Guess you like

Origin blog.csdn.net/weixin_47490310/article/details/114239819