欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

redis 解決key的亂碼問題,并清理詳解

 更新時間:2020年07月17日 08:36:47   作者:null_-  
這篇文章主要介紹了redis 解決key的亂碼問題,并清理詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

key亂碼問題

因redis默認(rèn)使用JdkSerializationRedisSerializer來進(jìn)行序列化,造成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的亂碼問題解決,含日期轉(zhuǎn)化格式問題

在項目中,遇到的問題是redis的key和value出現(xiàn)的亂碼問題:在這里插入圖片描述

而原本的內(nèi)容為下:

{
  "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內(nèi)部的編碼協(xié)議出現(xiàn)了問題,所以需要改進(jìn)。spring提供了一個優(yōu)化方案。springboot的redisTemplate改進(jìn)。

@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的數(shù)據(jù)比以前多了,這是因為日期的轉(zhuǎn)化出現(xiàn)問題,這塊的知識觸及盲區(qū),就先把解決方案寫下面,以后有時間在研究。

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);//轉(zhuǎn)成
  }
}
@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);

    //改進(jìn)日期轉(zhuǎn)化問題
    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;
  }
}

最后終于出現(xiàn)了預(yù)期的效果

以上這篇redis 解決key的亂碼問題,并清理詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Redis使用RedisTemplate導(dǎo)致key亂碼問題解決

    Redis使用RedisTemplate導(dǎo)致key亂碼問題解決

    本文主要介紹了Redis使用RedisTemplate導(dǎo)致key亂碼問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • win 7 安裝redis服務(wù)【筆記】

    win 7 安裝redis服務(wù)【筆記】

    Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。
    2016-05-05
  • Redis中的動態(tài)字符串學(xué)習(xí)教程

    Redis中的動態(tài)字符串學(xué)習(xí)教程

    這篇文章主要介紹了Redis中的動態(tài)字符串學(xué)習(xí)教程,以sds模塊的使用為主進(jìn)行講解,需要的朋友可以參考下
    2015-08-08
  • redis刪除hash的實現(xiàn)方式

    redis刪除hash的實現(xiàn)方式

    這篇文章主要介紹了redis刪除hash的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 使用Spring?Boot實現(xiàn)Redis鍵過期回調(diào)功能示例詳解

    使用Spring?Boot實現(xiàn)Redis鍵過期回調(diào)功能示例詳解

    這篇文章主要介紹了使用Spring?Boot實現(xiàn)Redis鍵過期回調(diào)功能,就是一個實現(xiàn)Redis鍵過期回調(diào)功能的Spring?Boot應(yīng)用的示例,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • 一篇文章揭秘Redis的磁盤持久化機制

    一篇文章揭秘Redis的磁盤持久化機制

    這篇文章主要給大家介紹了如何通過一篇文章揭秘Redis的磁盤持久化機制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 全網(wǎng)最完整的Redis新手入門指導(dǎo)教程

    全網(wǎng)最完整的Redis新手入門指導(dǎo)教程

    這篇文章主要給大家介紹了Redis新手入門的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 基于?Spring?Aop?環(huán)繞通知實現(xiàn)?Redis?緩存雙刪功能(示例代碼)

    基于?Spring?Aop?環(huán)繞通知實現(xiàn)?Redis?緩存雙刪功能(示例代碼)

    基于 spring aop 常規(guī)應(yīng)用場景多是用于日志記錄以及實現(xiàn) redis 分布式鎖,在 github 中也有項目是把它拿來當(dāng)作緩存的異常捕捉,這篇文章主要介紹了基于?Spring?Aop?環(huán)繞通知實現(xiàn)?Redis?緩存雙刪,需要的朋友可以參考下
    2022-08-08
  • Redis如何實現(xiàn)分布式鎖

    Redis如何實現(xiàn)分布式鎖

    相信大家對鎖已經(jīng)不陌生了,本文主要介紹了Redis如何實現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 簡介Lua腳本與Redis數(shù)據(jù)庫的結(jié)合使用

    簡介Lua腳本與Redis數(shù)據(jù)庫的結(jié)合使用

    這篇文章主要介紹了簡介Lua腳本與Redis數(shù)據(jù)庫的結(jié)合使用,Redis是基于主存的高性能數(shù)據(jù)庫,需要的朋友可以參考下
    2015-06-06

最新評論