2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

解決使用redisTemplate set方法保存出現(xiàn)\x00\問題

 小世界的野孩子 2022-03-17

在項(xiàng)目有個(gè)需求要保存一個(gè)字符串到redis,并設(shè)置一個(gè)過期時(shí)間。這個(gè)需求一看非常簡單,使用redisTemplate一行代碼搞定,代碼如下

redisTemplate.opsForValue().set("userKey", data, 10000); 

但保存后,查看redis發(fā)現(xiàn)value的前綴多出了

\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x

一開始以為是redis的序列化問題,于是就修改了redisTemplate的序列化方式,終于還是沒能解決問題。那問題出在哪里?翻看源碼,發(fā)現(xiàn)redisTemplate.opsForValue().set()有重載方法,一個(gè)是

void set(K key, V value, long offset) 

另外一個(gè)是

void set(K key, V value, long timeout, TimeUnit unit)

調(diào)用set(K key, V value, long offset)這個(gè)方法,其底層調(diào)用的是redis的setrange命令,這個(gè)命令看官網(wǎng)介紹

Overwrites part of the string stored at key, starting at the specified offset, for the entire length of value. If the offset is larger than the current length of the string at key, the string is padded with zero-bytes to make offset fit. Non-existing keys are considered as empty strings, so this command will make sure it holds a string large enough to be able to set value at offset

其含義是從指定的偏移量開始,覆蓋整個(gè)值范圍內(nèi)從key存儲(chǔ)的字符串的一部分。如果偏移量大于key處字符串的當(dāng)前長度,則該字符串將填充零字節(jié)以使偏移量適合。不存在的鍵被視為空字符串,因此此命令將確保它包含足夠大的字符串以能夠?qū)⒅翟O(shè)置為offset。

調(diào)用set(K key, V value, long timeout, TimeUnit unit)這個(gè)方法,其底層調(diào)用的是redis命令setex。這個(gè)命令看官網(wǎng)介紹

Set key to hold the string value and set key to timeout after a given number of seconds

很顯然這個(gè)方法,才是我們真正想要的方法。因此解決使用restemplate set方法保存出現(xiàn)\x00\問題的方案就是使用

void set(K key, V value, long timeout, TimeUnit unit)

這個(gè)方法

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多