Getting started with Redis –jedis storing Java objects-(Java serialized as a byte array)

Source: Internet
Author: User

Getting Started with Redis –jedis storing Java objects-(Java serialized as a byte array)

Original address: http://alanland.iteye.com/admin/blogs/1600685(Welcome reprint- reproduced please keep the original link)

07/19/12 03:08:05 PM

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

1 objects to store

Now write a very native Java Beanthat contains two fields, ID and name, andthe class name is called Person. To implement serialization requirements, the class implements the Serializable interface.

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 serialization of objects and the work of rice serialization. The code is as follows:

public class Serializeutil {

Public Static byte[] Serialize (Object object) {

ObjectOutputStream oos = null;

Bytearrayoutputstream BAOs = null;

try {

// serialization

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 the person object to Redis :

public void SetObject () {

Person person = new Person ("Alan");

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

Person = new Person (101, "Bruce");

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

}

After running the above code, we read the object to the command-line window to see if there was a successful write:

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 "

The value after the serialization can be taken.

4 fetching Objects

To get an object with Jedis:

Public person getObject (int id) {

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

return (person) serializeutil.unserialize (person);

}

Test the two objects saved 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

This shows that the serialized object is accessed correctly in Redis.

Getting started with Redis –jedis storing Java objects-(Java serialized as a byte array)

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.