Store Java objects to the Redis database (using serialization and deserialization)

Source: Internet
Author: User
Tags redis serialization

Redis does not support the direct storage of Java objects to a database, so you need to serialize Java objects to a byte array, and then deposit the byte array into the Redis, and then take the byte array out of the Redis database when you need the data. Then deserialization converts its own array to object use (JDK serialization performance is worse than Google's Protobuf serialization performance, and the serialized byte length is also a bit longer, so it is recommended to use protobuf,protobuf how to serialize, see my other post)
entity Classes
User.java
[Java]  View Plain  copy import java.io.serializable;   public class user  implements serializable{   private static final long serialversionuid =  2724888087391664167L;   private string id;   private string  username;   private string password;   public user ()  {  }    Public user (String id, string username, string password)  {    this.id = id;   this.username = username;   This.password  = password;  }   Public string getid ()  {   return  id;  }   Public void setid (string id)  {   this.id =  id;  }   public string getusername ()  {   return username;   }   public void setusername (string username)  {   this.username =  username;  }   Public string getpassword ()  {   Return password ;  }   Public void setpassword (String password)  {   this.password = password;  }   public static long  Getserialversionuid ()  {   return serialversionuid;  }   @Override     public string tostring ()  {   return  "user [id="  + id +   ",  username="  + username +  ",  password="    + password +   "]";  }  }   serialization Deserialization tool class
Serializeutils.java
[Java]View plain copy import Java.io.ByteArrayInputStream;   Import Java.io.ByteArrayOutputStream;   Import java.io.IOException;   Import Java.io.ObjectInputStream;   Import Java.io.ObjectOutputStream; public class Serializeutils {

Related Article

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.