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

Can redis save objects?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Can redis save objects? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Redis can store objects, but serialization and deserialization are needed.

Why implement a serialization interface?

When a class implements the Serializable interface (which is only a tagged interface and does not contain any method definitions), it means that the class can be serialized. The purpose of serialization is to convert an object that implements the Serializable interface into a sequence of bytes. Save the byte sequence (for example, in a file), and you can restore the byte sequence to the original object at any time. The byte sequence can even be restored to other computers or transferred to other computers through the network, and it can be restored to the original object as long as the corresponding class exists in the computer platform. Implementation: to serialize an object, you first create some OutputStream objects, then encapsulate them in an ObjectOutputStream object, and then call the writeObject () method to serialize an object; deserialization is similar.

Note: writing to a file using an object stream ensures that not only is the object serialized, but the object's member objects must also be serialized.

About serialVersionUID in the class of the Serializable interface:

SerialVersionUID is of type long. There are two ways to generate in Eclipse:

The default is 1L:

Private static final long serialVersionUID = 1L

The other is to generate a 64-bit hash field based on class name, interface name, member methods, properties, and so on:

Private static final long serialVersionUID = 3969438177161438988L

SerialVersionUID is mainly to solve the compatibility problem of object deserialization.

If no serialVersionUID is provided, the filed of the class is increased or decreased after the object is serialized and saved to the hard disk. In this way, when deserialization occurs, Exception occurs, causing incompatibility problems.

But when the serialVersionUID is the same, it deserializes the different field with the default value of type. In this way, the incompatibility problem can be avoided.

The above methods can only be restored to Java objects, if you want to restore to other objects (such as C++ objects), you need to convert the Java objects to XML format, so that they can be used by a variety of platforms and languages. You can use the javax.xam.* class library released with JDK, or you can use the open source XOM class library.

Experimental case:

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) / / storing data jedis.set (keys, "zy"); / fetching data String value = jedis.get (keys); System.out.println (value); / / storing object Person p=new Person (); / / peson class remembering to implement serialization interface Serializable p.setAge (20); p.setName ("Yao Bo") 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;}} this is the answer to the question about whether redis can save objects. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

Database

Wechat

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

12
Report