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
Configure redis (including clustering and serialization) in .xml, inject RedisTemplate into the class, and use asynchronous callback to operate redis (or operate directly)
RedisTemplate