Redis uses the serialization scheme to store objects, and redis serializes

Source: Internet
Author: User

Redis uses the serialization scheme to store objects, and redis serializes

Reprinted from https://www.cnblogs.com/yaobolove/p/5632891.html

A few days ago, I was asked such a question: how to store objects in redis? I didn't pay much attention to it at ordinary times. I only know the characters stored in redis, but it depends on the key access value. However, the object is still different.

First, let's take a look at why serialization is required.

Why serialized interfaces?

When a class implements the Serializable interface (this interface is only a tag interface and does not contain any method definition), it indicates that the class can be serialized. the purpose of serialization is to convert an object that implements the Serializable interface into a byte sequence. Save the byte sequence (for example, saving it in a file), and you can restore the sequence to the original object at any time. You can even put the byte sequence on another computer or transmit it to another computer over the network for restoration. As long as the computing platform has a corresponding class, it can be restored to the original object. Implementation: to serialize an object, you must first create some OutputStream objects, encapsulate them in an ObjectOutputStream object, and then call the writeObject () method to serialize an object. deserialization is similar.
Note: writing an object stream to a file not only ensures that the object is serialized, but also the member object of the object must be serialized.

About serialVersionUID In the Serializable interface class:
SerialVersionUID is of the long type. There are two generation methods in Eclipse:
The default value is 1L:
Private static final long serialVersionUID = 1L;
The other is to generate a 64-bit hash field based on the class name, Interface Name, member method, and attribute:
Private static final long serialVersionUID = 415438177161438988l;
SerialVersionUID is mainly used to solve the compatibility problem of object deserialization.
If serialVersionUID is not provided, after the object is serialized and saved to the hard disk, the filed of the class is added or reduced. In this way, Exception occurs during deserialization, resulting in incompatibility issues.
However, when the serialVersionUID is the same, it deserializes different fields with the default value of type. This avoids incompatibility issues.

The above method can only be restored to a Java object. If you want to restore to another object (such as a C ++ object), you need to convert the Java object to the XML format, in this way, it can be used by various platforms and languages. You can use the javax. xam. * Class Library released along with JDK, or use the open-source XOM class library (you can download and obtain documents from www. xom. nu ).

 

Next, let's take a look at how redis stores objects.

Package Object1; import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. objectInputStream; import java. io. objectOutputStream; import bean. person; import redis. clients. jedis. jedis; public class SerializeUtil {public static void main (String [] args) {Jedis jedis = new Jedis ("172.16.135.2"); String keys = "name "; // delete data // jedis. del (keys); // Save the data jedis. set (keys, "zy"); // obtain the data String value = jedis. get (keys); System. out. println (value); // save object Person p = new Person (); // the peson class remembers to implement the Serializable p serialization interface. setAge (20); p. setName ("Zhang San"); p. setId (1); jedis. set ("person ". getBytes (), serialize (p); byte [] byt = jedis. get ("person ". getBytes (); Object obj = unserizlize (byt); if (obj instanceof Person) {System. out. println (obj) ;}}// serialize public static byte [] serialize (Object obj) {ObjectOutputStream obi = null; ByteArrayOutputStream bai = null; try {bai = new ByteArrayOutputStream (); obi = new ObjectOutputStream (bai); obi. writeObject (obj); byte [] byt = bai. toByteArray (); return byt;} catch (IOException e) {e. printStackTrace ();} return null;} // deserialize public static Object unserizlize (byte [] byt) {ObjectInputStream oii = null; ByteArrayInputStream bis = null; bis = new ByteArrayInputStream (byt); try {oii = new ObjectInputStream (bis); Object obj = oii. readObject (); return obj;} catch (Exception e) {e. printStackTrace ();} return null ;}}

Reprinted from https://www.cnblogs.com/yaobolove/p/5632891.html

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.