springboot - Why is the spring-data-redis library so troublesome to use? You have to write a serialization class once for each class. Did I use it wrong??
伊谢尔伦
伊谢尔伦 2017-06-28 09:27:25
0
1
1183

Declare a serialization class that handles a certain type (BaseCurrentUser)

    @Bean
    Jackson2JsonRedisSerializer<BaseCurrentUser> jackson2JsonRedisSerializer(ObjectMapper objectMapper) {
        Jackson2JsonRedisSerializer<BaseCurrentUser> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<BaseCurrentUser>(
                BaseCurrentUser.class);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return jackson2JsonRedisSerializer;
    }

    @Bean
    RedisTemplate<String, BaseCurrentUser> currentUserRedisTemplate(JedisConnectionFactory connectionFactory,
                                                                    Jackson2JsonRedisSerializer<BaseCurrentUser> jackson2JsonRedisSerializer) {
        RedisTemplate<String, BaseCurrentUser> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        return redisTemplate;
    }

Used in service class

@Autowired
private RedisTemplate<String, BaseCurrentUser> currentUserRedisTemplate;

Then every time I add a type of storage, I have to add a set of @Bean??? Is that so? Or I don’t know how to use it?
Or I don’t use it this way, I only use its StringRedisTemplate, every time All are manually serialized using fastjson

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
我想大声告诉你

Configure redis (including clustering and serialization) in .xml, inject RedisTemplate into the class, and use asynchronous callback to operate redis (or operate directly)

RedisTemplate

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template