Home >Database >Redis >Can redis save objects?

Can redis save objects?

anonymity
anonymityOriginal
2019-06-04 16:25:013001browse

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

Can redis save objects?

Why should we implement the serialization interface?

When a class implements the Serializable interface (this interface is only a mark interface and does not contain any method definitions), it means that the class can be serialized. The purpose of serialization is to convert a class that implements the Serializable interface into a The object can be converted into a byte sequence. Save the byte sequence (for example, in a file), and you can restore the byte sequence to the original object at any time later. The byte sequence can even be put on other computers or transmitted to other computers through the network for recovery. As long as the corresponding class exists on the computer platform, it can be restored to the original object normally. Implementation: To serialize an object, you must 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: When using an object stream to write to a file, not only must the object be serialized, but the member objects of the object must also be serialized.

About the class of the Serializable interface The serialVersionUID:

serialVersionUID is of long type. There are two generation methods in Eclipse:

The default is 1L:

private static final long serialVersionUID = 1L;

The other one is based on the class name and interface name , member methods and attributes, etc. generate a 64-bit hash field:

private static final long serialVersionUID = 3969438177161438988L;

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

If serialVersionUID is not provided, the field of the class will be increased or decreased after the object is serialized and stored on the hard disk. In this way, an Exception will occur when deserializing, causing incompatibility issues.

But when the serialVersionUID is the same, it will deserialize different fields with the default value of type. This way you can avoid incompatibility issues.

The above method can only be restored to Java objects. If you want to restore to other objects (such as C objects), you must convert the Java objects to XML format, so that they can be used by various platforms and various Language use. You can use the javax.xam.* class libraries distributed with the JDK, or use the open source XOM class library (can be downloaded and documented from www.xom.nu).

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";
        // 删数据
        //jedis.del(keys);
        // 存数据
        jedis.set(keys, "zy");
        // 取数据
        String value = jedis.get(keys);
        System.out.println(value);
        
        //存对象
        Person p=new Person();  //peson类记得实现序列化接口 Serializable
        p.setAge(20);
        p.setName("姚波");
        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);
        }
    }
    
    //序列化 
    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;
    }
    
    //反序列化
    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;
    }
}

The above is the detailed content of Can redis save objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn