java中redisTemplate操作redis

@Configuration
public class RedisConfiguration {


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

// 设置value的序列化规则和 key的序列化规则
redisTemplate.setValueSerializer(getSystemRedisSerializer());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();

return redisTemplate;
}

@Bean("systemRedisSerializer")
public RedisSerializer getSystemRedisSerializer() {
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
objectMapper.setDateFormat(simpleDateFormat);

jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
return jackson2JsonRedisSerializer;
}



ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("indexTotal:interfaceCallTotalNum", interfaceCallTotalNum);





java中redisTemplate操作redis

 

 

 

 

 



上一篇:使用 Jackson – 将字符串转换为 JsonNode 对象


下一篇:后端传Long类型的值给前端vue,导致精度丢失为问题的解决