宣告處理某個型別(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;
}
在service類別中使用
@Autowired
private RedisTemplate<String, BaseCurrentUser> currentUserRedisTemplate;
那麼我每次加上一個類型的儲存, 就得加一套@Bean??? 是這樣嘛? 還是我不會用?
或者說不這麼想, 只用他的StringRedisTemplate, 每次都用fastjson 手動序列化
在.xml中配置redis(包括叢集、序列化),在類別中註入RedisTemplate,使用非同步回呼的方式操作redis(或直接操作)
RedisTemplate