Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept of serialization and deserialization of Redis

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "the concept of serialization and deserialization of Redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Serialization concept

Serialization: the process of converting an object into a transferable byte sequence is called serialization.

Deserialization: the process of restoring a sequence of bytes to an object is called deserialization.

Why serialization is needed

The ultimate goal of serialization is to enable objects to be stored across platforms and transmitted over the network. The way we do cross-platform storage and network transmission is IO, and the data format supported by our IO is byte arrays.

Because we unilaterally only the object into a byte array is not enough, because there is no rule of the byte array we can not restore the true face of the object, so we must convert the object to a byte array when making a rule (serialization), then we read the data from the IO stream with this rule to restore the object back (deserialization).

If we are going to transport a house from one place to another, serialization is that I tear down the house into bricks and put them in the car, and then leave a drawing of the original structure of the house. Deserialization is the process of restoring the bricks to the original appearance of the house according to the drawings after we have transported the house to its destination.

When do you need serialization?

From the above, I think you already know that any data that requires "cross-platform storage" and "network transmission" needs to be serialized.

In essence, storage and network transmission need to save an object state into a cross-platform recognized byte format, and then other platforms can restore object information through byte information parsing.

Serialization is just a rule for disassembling and assembling objects, so there must be a variety of rules. For example, the common serialization methods are: JDK (does not support cross-language), JSON, XML, Hessian, Kryo (does not support cross-language), Thrift, Protostuff, FST (does not support cross-language).

Java serialization

Java implementation serialization is simple, you only need to implement the Serializable interface.

Note: common problems in JAVA serialization

Problem 1: the static property cannot be serialized

Reason: serialization saves the state of the object, and static variables belong to the state of the class, so serialization does not save static variables.

Problem 2: the Transient property will not be serialized

The function of the transient keyword of java is to implement the Serilizable interface, adding the keyword transient to the attribute that does not need to be serialized, and when serializing the object, this property will not be serialized to the specified destination.

Question 3: serialize version number serialVersionUID

All serialized objects must have a version number, which can be defined by ourselves. When we do not define it, the JDK tool will generate a corresponding version number according to the properties of our object. Using the serialVersionUID generated by JDK, the serialVersionUID will change as long as the object changes a little. Therefore, it is recommended that you manually define the version number.

Comparison of redis serialization methods:

The default way for redis is JdkSerializationRedisSerializer.

JdkSerializationRedisSerializer: use the serialization capabilities provided by JDK. The advantage is that you don't need to provide type information (class) when deserializing, but the disadvantage is that you need to implement the Serializable interface, and the serialized result is very large, about five times the size of the JSON format, which consumes a lot of memory on the redis server.

Jackson2JsonRedisSerializer: use the Jackson library to serialize objects into JSON strings. The advantage is that it is fast, the serialized string is short and short, and there is no need to implement the Serializable interface. But the disadvantage is also fatal, that is, there is a type parameter in the constructor of this class, which must provide the type information (.class object) of the object to be serialized. By looking at the source code, it is found that it only uses type information during deserialization.

Problem: using the default JDK serialization method, there will be "garbled" when viewing the kmurv value in the RDM tool, which is not convenient to view.

Solution: customize the serialization method, using Jackson2JsonRedisSerializer

Redis serialization

When you use Redis's key and value, value is a byte array to redis. You are responsible for converting your data structure into byte array and reading it when you read it.

One special case is the string, because the string itself is almost already byte array, so you don't have to deal with it yourself.

Spring's redisTemplate uses java serialization for serialization by default. You can also use StringRedisTemplate, so all the data of your set will be toString and then stored in redis. But this toString may not be able to reverse parsing back. If you use java native serialization, there may be problems with remote code execution, so it is recommended to use other serialization methods instead.

This is the end of "the concept of serialization and deserialization of Redis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report