Redis solves the key garbled problem and cleans up the detailed explanation

Time:2020-10-15

Key garbled code problem

Since redis uses the jdkserializationredisserializer by default, the key is garbled, as follows:

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”

Solve key garbled code


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;
 }

Clean up the garbled key

The official does not support the Del ‘* keys’ method to blur / batch delete keys. However, the keys command supports fuzzy matching, so the following methods are adopted:

Method 1: normal keys can be deleted, but random keys cannot be deleted

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

Method 2: delete successfully

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

Supplementary knowledge:Redis key and value garbled problem solved, including date conversion format

In the project, the problem is that the key and value of redis are garbled: insert a picture description here

The original contents are as follows:

{
  "status":"success",
  "data":{
    "id":3,
    "Title": "Hualin",
    "price":99,
    "stock":81,
    "Description": "a beauty",
    "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"
  }
}

Reason: there is a problem with the internal coding protocol of redis, so it needs to be improved. Spring provides an optimization solution. Springboot’s redistemplate is improved.

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

    //First, we solve the problem of key serialization
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringRedisSerializer);

    //Solve the serialization problem of value
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

    return redisTemplate;
  }
}

It’s better than before, but there are still some small problems. There are more data in JSON than before. This is because there is a problem in the conversion of date. If this knowledge touches the blind area, you should write down the solution first and then have time to study it.


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); // converted to
  }
}
@Component
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600)
public class RedisConfig {
  @Bean
  public RedisTemplate redisTemplate(RedisConnectionFactory factory){
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(factory);

    //First, we solve the problem of key serialization
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringRedisSerializer);

    //Solve the serialization problem of value
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

    //Improve the date conversion problem
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(DateTime.class,new JodaDateTimeJsonSerializer());
    simpleModule.addDeserializer(DateTime.class,new JodaDateTimeJsonDeserializer());

//Resolving the deserialization problem objectMapper.enableDefaultTyping ( ObjectMapper.DefaultTyping.NON_ FINAL);
    objectMapper.registerModule(simpleModule);
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

    return redisTemplate;
  }
}

Finally, the expected effect appeared

The above article redis solves the key’s garbled code problem and cleans up the detailed explanation, which is the entire content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developeppaer more.

Recommended Today

Choose react or angular 2

Original addressChoosing between React vs. Angular 2The following is the translation of this article, which can be used at your choiceReactperhapsAngular2We need to help when we need to. React has become a cool representative in 2015, but angular.js has changed from a front-end framework that people love to a terrible devil (and not so terrible…) […]