问题描述
声明处理某个类型(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类中使用
@Autowiredprivate RedisTemplate<String, BaseCurrentUser> currentUserRedisTemplate;
那么我每次添加一个类型的存储, 就得加一套@Bean??? 是这样嘛? 还是我不会用?或者说不这么用, 只用他的StringRedisTemplate, 每次都用 fastjson 手动序列化
问题解答
回答1:在.xml中配置redis(包括集群、序列化),在类中注入RedisTemplate,使用异步回调的方式操作redis(或者直接操作)
RedisTemplate