Jedis operations Redis serialization of stored objects

Source: Internet
Author: User
Tags object object redis serialization

In Jedis development, we often want to put an object directly into the Redis and then take it out when needed. Redis key and value both support binary secure strings, storing Java objects is not a problem, let's look at how to implement it.

1 Objects to store

Now write a very earthy Java Bean that contains two fields, ID and name, and the class name is called person. In order to achieve serialization requirements, this class implements the Serializable interface.

Java

public class Person implements Serializable {

private int id;

private String name;

public person (int ID, String name) {

This.id = ID;

THIS.name = name;

}

public int getId () {

return ID;

}

Public String GetName () {

return name;

}

}


2 serialization, deserialization

Write a serialization tool class that provides the task of serializing objects and sequencing the food. The code is as follows:

Java

public class Serializeutil {

public static byte[] Serialize (Object object) {

ObjectOutputStream oos = null;

Bytearrayoutputstream BAOs = null;

try {

Serialization of

BAOs = new Bytearrayoutputstream ();

Oos = new ObjectOutputStream (BAOs);

Oos.writeobject (object);

byte[] bytes = Baos.tobytearray ();

return bytes;

catch (Exception e) {

}

return null;

}

public static Object unserialize (byte[] bytes) {

Bytearrayinputstream Bais = null;

try {

Deserialization

Bais = new Bytearrayinputstream (bytes);

ObjectInputStream ois = new ObjectInputStream (Bais);

return Ois.readobject ();

catch (Exception e) {

}

return null;
}


3 Writing objects

To write a person object to the Redis:

Java

public void SetObject () {

person is = new Person ("Alan");

Jedis.set ("person:100". GetBytes (), serializeutil.serialize (person));

person = new Person ("Bruce");

Jedis.set ("person:101". GetBytes (), serializeutil.serialize (person));

}


After running the above code, we read the object in the command line window to see if there is a write success:

Redis 127.0.0.1:6379> Get person:100

"\xac\xed\x00\x05sr\x00\x15alanland.redis.person\x05\xf4\x8d9a\xf4 ' \xb0\x02\x00\x02i\x00\x02idl\x00\x04namet\ X00\x12ljava/lang/string;xp\x00\x00\x00dt\x00\x04alan "


You can take the value after serialization.


4 Fetching objects

To get an object using Jedis:

Java

Public person getObject (int id) {

byte[] person = jedis.get (("Person:" + ID). GetBytes ());

return [person] serializeutil.unserialize (person);

}

Test the two objects deposited in the previous step:

Person person = test.getobject (100);

System.out.println (Person.getid ());

System.out.println (Person.getname ());

person = test.getobject (101);

System.out.println (Person.getid ());

System.out.println (Person.getname ());

Java Console input:

100

Alan

101

Bruce

Thus, the serialized object is accessed correctly in the Redis.

Method test is feasible.

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.