Spring operation Redis When key value appears \XAC\XED\X00\X05T\X00\TB

Source: Internet
Author: User
Tags hash object object redis serialization

Recent research into Redis, and spring data support for Redis, has found a strange phenomenon

First say the phenomenon, through the redistemplate under the Opsforhash method to store the value of the hash type, after the successful operation, to the Redis console to display the keys *, found a strange phenomenon, the inserted hash type of key in front of a bunch of \xac\ XED\X00\X05T\X00\TB This kind of thing, see figure 1


(Fig. 1)

Did you see it. is the second line that a string of things they come out of, analysis Spring-data org.springframework.data.redis.core.RedisTemplate source code later found:

[Java] view plain copy private redisserializer<?> Defaultserializer = new      Jdkserializationredisserializer ();   Private Redisserializer Keyserializer = null;   Private Redisserializer ValueSerializer = null;   Private Redisserializer Hashkeyserializer = null;   Private Redisserializer Hashvalueserializer = null;   Private redisserializer<string> Stringserializer = new Stringredisserializer ();

Found a:

[Java] view plain copy private redisserializer<?> Defaultserializer = new Jdkserializationredisseria   Lizer ();

Because spring operations Redis is based on the Jedis client, while the Jedis client interacts with Redis, the protocol defines a byte type to interact with, Jedis provides a type of string to byte[]. But see Spring-data-redis in Redistemplate<k, v> in the operation of the time k,v is generic, so redistemplate in the above code, in the absence of a special definition of the case, Spring uses defaultserializer = new Jdkserializationredisserializer () by default, to serialize the Key,value, In a series of operations that have been serialized in the view Jdkserializationredisserializer, the following code is found:

[Java] view plain copy private converter<object, byte[]> serializer = new Serializingconverter ();       Public byte[] Serialize (Object object) {if (object = = null) {return serializationutils.empty_array;       } try {return Serializer.convert (object);       } catch (Exception ex) {throw new SerializationException ("Cannot serialize", ex); }   }  

Serialization supports object objects, calls the Convert method under the Serializingconverter class, and transforms the object by:

[Java]  View plain copy private final serializer<object> serializer;  /**   * serializes the source object and returns the byte array  result.   */   Public byte[] convert (object source)  {       bytearrayoutputstream bytestream = new bytearrayoutputstream (128);        try  {            this.serializer.serialize (source, bytestream);            return bytestream.tobytearray ();       }        catch  (Throwable ex)  {            Throw new serializationfailedexception ("failed to serialize object using "  + &nbsp                  This.serializer.getClass (). Getsimplename (),  ex);       }  }   

The reason is actually here, the solution is to manually define the method of serialization, Spring-data-redis also provides a serialized class specifically for the serialization of string type Org.springframework.data.redis.serializer.StringRedisSerializer, which can be used in XML to refer to the Availability

[HTML]  View plain copy <bean id= "redistemplate"  class= " Org.springframework.data.redis.core.RedisTemplate "       p:connection-factory-ref = "Jedisconnectionfactory" >       <property name= "Keyserializer" >            <bean class= " Org.springframework.data.redis.serializer.StringRedisSerializer " />        </property>       <property name= "ValueSerializer" >            <bean class= " Org.springframework.data.redis.serializer.StringRedisSerializer " />        </property>       <property name= "Hashkeyserializer" >            <bean class= " Org.springframework.data.redis.serializer.StrIngredisserializer " />       </property>        <property name= "Hashvalueserializer" >            <bean class= "Org.springframework.data.redis.serializer.StringRedisSerializer"  />        </property>   </bean>  

Where I have specified the Key,value and hash type key,value of the base type in Redis as the Stringredisserializer class for serialization, and then found an error, and found my code:

[Java] view plain copy public void Setbank (final bank) {//TODO Setbank final String key       = String.Format ("bank:%s", Bank.getid ());              Final Map<string, object> properties = new hashmap<string, object> ();       Properties.put ("id", Bank.getid ());              Properties.put ("Name", Bank.getbank_name ());   Redistemplate.opsforhash (). Putall (Key, properties); }  

Bank.getid is of type integer because the key and value in HashMap are also serialized, and the Serialize method in Stringredisserializer only receives a string of value, as follows:

[Java] view plain copy public byte[] Serialize (string string) {return (string = = null? null:strin   G.getbytes (charset)); }  

So here's the type conversion error, apart direct Bank.getid (). toString (), execute successfully, and then go to the Redis console open to see that the heap of disgusting \xac\xed\x00\x05t\x00\tb is gone.


(Red part)

In fact, Redis in use, must be considered in advance is the memory of the problem (another article in the blog is very classic, elaborated a foreigner to do the test, the same storage can save dozens of g of memory "Redis memory Optimization" Save memory: Instagram's redis practice), In the design of the use of hash can save a lot of memory, and when stored if each piece of data put a heap of \XAC\XED\X00\X05T\X00\TB this thing, must not look straight

————————————————————————————————

Above is a personal humble opinion, if there is a wrong place to analyze, welcome to make bricks


Original address: http ://blog.csdn.net/yunhaibin/article/details/9001198

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.