解決使用redisTemplate set方法保存出現\x00\問題

在項目有個需求要保存一個字符串到redis,並設置一個過時時間。這個需求一看很是簡單,使用redisTemplate一行代碼搞定,代碼以下redis

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

但保存後,查看redis發現value的前綴多出了ide

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

一開始覺得是redis的序列化問題,因而就修改了redisTemplate的序列化方式,終於仍是沒能解決問題。那問題出在哪裏?翻看源碼,發現redisTemplate.opsForValue().set()有重載方法,一個是this

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

另一個是rest

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

調用set(K key, V value, long offset)這個方法,其底層調用的是redis的setrange命令,這個命令看官網介紹code

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 offsetci

其含義是從指定的偏移量開始,覆蓋整個值範圍內從key存儲的字符串的一部分。若是偏移量大於key處字符串的當前長度,則該字符串將填充零字節以使偏移量適合。不存在的鍵被視爲空字符串,所以此命令將確保它包含足夠大的字符串以可以將值設置爲offset。字符串

調用set(K key, V value, long timeout, TimeUnit unit)這個方法,其底層調用的是redis命令setex。這個命令看官網介紹源碼

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

很顯然這個方法,纔是咱們真正想要的方法。所以解決使用restemplate set方法保存出現\x00\問題的方案就是使用it

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

這個方法

相關文章
相關標籤/搜索