redis and serialization

concept

Serialization: the object into a byte sequence of process may be referred to serialized transmission.

Deserialize: the reduction process is a sequence of bytes is called object deserialization.

 

Why do we need to serialize

The ultimate goal is to serialize the object can be stored across platforms, and network transmission. And the way we carry out cross-platform storage and network transmission is IO, IO support and our data format is a byte array.

Because we unilaterally only the object into a byte array is not enough, because there are no rules byte array we have no way to restore the true colors of objects back, so we have to put object into a byte array when he develop a rule (serialization), then we read data from an IO stream inside again when the target reduction in this rule back (deserialization).

If we want a house transported from one place to another place, serialization is my house is split into a number of brick into the car, then left a drawing of the original structure of the house, is deserialized after the house we transport to the destination, according to the drawings of a block to restore the original appearance of the brick house to process

 

Under what circumstances need to serialize

Through the above I think you already know all the data needs to be "cross-platform storage" and "transmission network", needs to be serialized.

Essentially storage and transport have to go through the network to save the state of an object into a byte format recognizable cross platform and other platforms can be resolved only by reducing the object information byte information.

 

Serialization way

Serialization rules just a disassembly assembling objects, then this rule may certainly have a variety, such as the now common serialization methods are:

JDK (not supported across languages), JSON, XML, Hessian, Kryo (does not support cross-language), Thrift, Protostuff, FST (does not support cross-language)

 

Java serialization

java achieve serialization is very simple, we only need to implement the Serializable interface.

public class User implements Serializable{
 //年龄
 private int age;
 //名字
 private String name ;

 public int getAge() {
 return age;
    }
 public void setAge(int age) {
 this.age = age;
    }

 public String getName() {
 return name;
    }

 public void setName(String name) {
 this.name = name;
    }
}

  

Note: JAVA serialization common problem

  • A question: static property can not be serialized

 Reason: the sequence of the object's state is saved, the static state variable belonging to the class, so the sequence of static variables are not saved.

  • Second problem: Transient property will not be serialized
  • Question three: the serialized version serialVersionUID

 All objects that implement serialization must have a version number, the version number can be defined by ourselves, when we have not defined JDK tool generates a corresponding version number of the properties in accordance with our object. Use serialVersionUID JDK generated, as long as the object has changed a little bit with the serialVersionUID will change. It is recommended to manually define the version number.

 

Redis serialization

When you use the Redis key and value, value for redis terms it is a byte array. You have to put yourself in charge of converting your data structure into a byte array, read out when reading and so on.

A special case is string, because the string is already own almost byte array, so do not need to deal with.

Spring will use the default java serialization redisTemplate do serialization. You can also use StringRedisTemplate, then all of the data that you set will be saved to the toString a little longer in redis. But not necessarily anti-analytic back this toString. If you use a java native serialization, there may be a remote code execution problems, it is recommended to use other serialization instead.


233

Reference article


https://zhuanlan.zhihu.com/p/40462507

https://www.zhihu.com/question/277363840/answer/392945240

 

Guess you like

Origin www.cnblogs.com/lemos/p/11081448.html