Spring Data操作Redis時,發現key值出現 \xac\xed\x00\x05t\x00\tb

來源:互聯網
上載者:User

標籤:控制台   jdk   經典   部落格   comm   噁心   clipboard   tail   bank   

原文連結:http://blog.csdn.net/yunhaibin/article/details/9001198

最近在研究redis,以及spring data對redis的支援發現了一個奇怪的現象

先說現象吧,通過redisTemplate下的opsForHash方法儲存hash類型的值,操作成功以後,去redis控制台顯示keys * 的時候,發現一個奇怪的現象,插入的hash類型的key前面會有一堆的\xac\xed\x00\x05t\x00\tb 這種東西,見圖1

 

(圖1)

 

看見了嗎?就是第二行那一串自己冒出來的東西,分析spring-data的org.springframework.data.redis.core.RedisTemplate原始碼以後發現:

 

[java] view plain copy 
  1. private RedisSerializer<?> defaultSerializer = new JdkSerializationRedisSerializer();  
  2.   
  3. private RedisSerializer keySerializer = null;  
  4. private RedisSerializer valueSerializer = null;  
  5. private RedisSerializer hashKeySerializer = null;  
  6. private RedisSerializer hashValueSerializer = null;  
  7. private RedisSerializer<String> stringSerializer = new StringRedisSerializer();  

發現了一個:

 

[java] view plain copy 
  1. private RedisSerializer<?> defaultSerializer = new JdkSerializationRedisSerializer();  

 

因為spring操作redis是在jedis用戶端基礎上進行的,而jedis用戶端與redis互動的時候協議中定義是用byte類型互動,jedis中提供了string類型轉為byte[]類型,但是看到spring-data-redis中RedisTemplate<K, V>在操作的時候k,v是泛型的,所以RedisTemplate中有了上面那段代碼,在沒有特殊定義的情況下,spring預設採用defaultSerializer = new JdkSerializationRedisSerializer();來對key,value進行序列化操作,在經過查看JdkSerializationRedisSerializer中對序列化的一系列操作,發現如下代碼:

 

[java] view plain copy 
  1. private Converter<Object, byte[]> serializer = new SerializingConverter();  
  2. public byte[] serialize(Object object) {  
  3.     if (object == null) {  
  4.         return SerializationUtils.EMPTY_ARRAY;  
  5.     }  
  6.     try {  
  7.         return serializer.convert(object);  
  8.     } catch (Exception ex) {  
  9.         throw new SerializationException("Cannot serialize", ex);  
  10.     }  
  11. }  

 

序列化支援的是Object對象,調用了SerializingConverter類下的convert方法轉換對象,轉換對象的方法是:

 

[java] view plain copy 
  1. private final Serializer<Object> serializer;  
  2. /** 
  3.  * Serializes the source object and returns the byte array result. 
  4.  */  
  5. public byte[] convert(Object source) {  
  6.     ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);  
  7.     try  {  
  8.         this.serializer.serialize(source, byteStream);  
  9.         return byteStream.toByteArray();  
  10.     }  
  11.     catch (Throwable ex) {  
  12.         throw new SerializationFailedException("Failed to serialize object using " +  
  13.                 this.serializer.getClass().getSimpleName(), ex);  
  14.     }  
  15. }  

 

原因其實就出現在這裡,解決的辦法就是手動定義序列化的方法,spring-data-redis中還提供了一個序列化的類專門針對string類型的序列化org.springframework.data.redis.serializer.StringRedisSerializer這個類,可以在xml裡面指定:

 

[html] view plain copy 
  1. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"  
  2.     p:connection-factory-ref="jedisConnectionFactory">  
  3.     <property name="keySerializer">  
  4.         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  5.     </property>  
  6.     <property name="valueSerializer">  
  7.         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  8.     </property>  
  9.     <property name="hashKeySerializer">  
  10.         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  11.     </property>  
  12.     <property name="hashValueSerializer">  
  13.         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  14.     </property>  
  15. </bean>  

 

其中我將redis中基礎類型的key,value和hash類型的key,value都指定為StringRedisSerializer類來進行序列化,然後發現又報錯了,找了一會發現My Code中:

 

[java] view plain copy 
  1. public void setBank(final Bank bank) {  
  2.     // TODO setBank  
  3.     final String key = String.format("bank:%s", bank.getId());  
  4.     final Map<String, Object> properties = new HashMap<String, Object>();  
  5.       
  6.     properties.put("id", bank.getId());  
  7.     properties.put("name", bank.getBank_name());  
  8.       
  9.     redisTemplate.opsForHash().putAll(key, properties);  
  10. }  

 

bank.getId是Integer類型的,因為Hashmap中的key和value也會進行序列化操作,而StringRedisSerializer中的serialize方法只接收String類型的值,如下:

 

[java] view plain copy 
  1. public byte[] serialize(String string) {  
  2.     return (string == null ? null : string.getBytes(charset));  
  3. }  

 

所以這裡的類型轉換報錯了,二話不說直接bank.getId().toString(),執行成功,然後進入redis控制台開啟查看發現那堆噁心巴拉的\xac\xed\x00\x05t\x00\tb沒有了,大功告成了

         

(紅色部分)

 

其實redis在使用中,必須要提前考慮的就是記憶體的佔用問題(部落格中另外一篇文章很經典,闡述了一個外國人做的測試,同樣的儲存可以節約幾十G的記憶體【Redis 記憶體最佳化】節約記憶體:Instagram的Redis實踐),在設計的時候使用hash可以節約很多的記憶體,而儲存的時候如果每條資料就放一堆\xac\xed\x00\x05t\x00\tb這個東西,肯定是無法直視的

 

------------------------------------------------------------------------------------------------

Spring Data操作Redis時,發現key值出現 \xac\xed\x00\x05t\x00\tb

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.