redis 解决key的乱码问题,并清理详解

xiancaione 2020-08-17

key乱码问题

因redis默认使用JdkSerializationRedisSerializer来进行序列化,造成key是乱码,如下:

keys '*!report:flag:phon*'

1) "\xac\xed\x00\x05t\x00!report:flag:phone_156464"

2) "\xac\xed\x00\x05t\x00!report:flag:phone_198946"

3) "\xac\xed\x00\x05t\x00!report:flag:phone_183302"

解决key乱码

private RedisTemplate redisTemplate;

 @Autowired(required = false)
 public void setRedisTemplate(RedisTemplate redisTemplate) {
  RedisSerializer stringSerializer = new StringRedisSerializer();
  redisTemplate.setKeySerializer(stringSerializer);
  redisTemplate.setValueSerializer(stringSerializer);
  redisTemplate.setHashKeySerializer(stringSerializer);
  redisTemplate.setHashValueSerializer(stringSerializer);
  this.redisTemplate = redisTemplate;
 }

清理乱码key

官方不支持 del '*keys'方式模糊/批量删除key。但是keys命令支持模糊匹配,所以采取以下方式:

方式1:可删除正常key,无法删除乱码key

redis-cli -h 192.168.1.21 -a password -n 2 --scan --pattern '*!report:flag:phon*' | xargs redis-cli -h 192.168.1.21 -a password -n 2 DEL

方式2:成功删除

del "\xac\xed\x00\x05t\x00!report:flag:phone_183302" "\xac\xed\x00\x05t\x00!report:flag:phone_198946"

补充知识:redis key和value的乱码问题解决,含日期转化格式问题

在项目中,遇到的问题是redis的key和value出现的乱码问题:在这里插入图片描述

redis 解决key的乱码问题,并清理详解

而原本的内容为下:

{
  "status":"success",
  "data":{
    "id":3,
    "title":"花林",
    "price":99,
    "stock":81,
    "description":"美女一只",
    "sales":17,
    "imgUrl":"https://xiaolei1996.oss-cn-shanghai.aliyuncs.com/blog/title/we1.jpg",
    "promoStatus":2,
    "promoPrice":50,
    "promoId":1,
    "startDate":"2020-03-23 21:50:59"
  }
}

原因: 是因为和redis内部的编码协议出现了问题,所以需要改进。spring提供了一个优化方案。springboot的redisTemplate改进。

@Component
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600)
public class RedisConfig {
  @Bean
  public RedisTemplate redisTemplate(RedisConnectionFactory factory){
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(factory);

    //首先解决key的序列化问题
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringRedisSerializer);

    //解决value的序列化问题
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

    return redisTemplate;
  }
}

比之前好了,但是还有点小问题,json的数据比以前多了,这是因为日期的转化出现问题,这块的知识触及盲区,就先把解决方案写下面,以后有时间在研究。

redis 解决key的乱码问题,并清理详解

public class JodaDateTimeJsonSerializer extends JsonSerializer<DateTime> {
  @Override
  public void serialize(DateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeString(value.toString("yyyy-MM-dd HH:mm:ss"));

  }
}
public class JodaDateTimeJsonDeserializer extends JsonDeserializer<DateTime> {
  @Override
  public DateTime deserialize(JsonParser p, DeserializationContext ctxt
  ) throws IOException, JsonProcessingException {
    String dateString= p.readValueAs(String.class);
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

    return DateTime.parse(dateString,dateTimeFormatter);//转成
  }
}
@Component
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600)
public class RedisConfig {
  @Bean
  public RedisTemplate redisTemplate(RedisConnectionFactory factory){
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(factory);

    //首先解决key的序列化问题
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringRedisSerializer);

    //解决value的序列化问题
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

    //改进日期转化问题
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(DateTime.class,new JodaDateTimeJsonSerializer());
    simpleModule.addDeserializer(DateTime.class,new JodaDateTimeJsonDeserializer());

//解决反序列化问题	objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    objectMapper.registerModule(simpleModule);
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

    return redisTemplate;
  }
}

最后终于出现了预期的效果

redis 解决key的乱码问题,并清理详解

相关推荐

DiamondTao / 0评论 2020-08-30