java操作redis存储对象

在Jedis开发中,我们很多时候希望直接把一个对象放到Redis中,然后在需要的时候取出来。Redis的key和value都支持二进制安全的字符串,存储Java对象不是问题,下面我们看一下如何来实现。

1要存储的对象
现在写一个很土的Java Bean,包含两个字段,id和name,类名叫做Person。为了实现序列化需求,该类实现Serializable接口。

  1. public class Person implements Serializable {  
  2. private int id;  
  3. private String name;  
  4.   
  5. public Person(int id, String name) {  
  6.   this.id = id;  
  7.   this.name = name;  
  8. }  
  9.   
  10. public int getId() {  
  11.   return id;  
  12. }  
  13.   
  14. public String getName() {  
  15.   return name;  
  16. }  
  17.   
  18. }  
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序列化、反序列化
写一个序列化工具类,来提供对象的序列化和饭序列化的工作。代码如下:

  1. public class SerializeUtil {  
  2. public static byte[] serialize(Object object) {  
  3. ObjectOutputStream oos = null;  
  4. ByteArrayOutputStream baos = null;  
  5. try {  
  6. //序列化  
  7. baos = new ByteArrayOutputStream();  
  8. oos = new ObjectOutputStream(baos);  
  9. oos.writeObject(object);  
  10. byte[] bytes = baos.toByteArray();  
  11. return bytes;  
  12. catch (Exception e) {  
  13. }  
  14. return null;  
  15. }  
  16.   
  17. public static Object unserialize(byte[] bytes) {  
  18. ByteArrayInputStream bais = null;  
  19. try {  
  20. //反序列化  
  21. bais = new ByteArrayInputStream(bytes);  
  22. ObjectInputStream ois = new ObjectInputStream(bais);  
  23. return ois.readObject();  
  24. catch (Exception e) {  
  25. }  
  26. return null;  
  27. }  
  28. }  
public class SerializeUtil {
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
//序列化
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 {
//反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}
return null;
}
}


3写对象
将Person对象写入Redis中:

  1. public void setObject() {  
  2. Person person = new Person(100“alan”);  
  3. jedis.set(”person:100”.getBytes(), SerializeUtil.serialize(person));  
  4. person = new Person(101“bruce”);  
  5. jedis.set(”person:101”.getBytes(), SerializeUtil.serialize(person));  
  6. }  
public void setObject() {
Person person = new Person(100, "alan");
jedis.set("person:100".getBytes(), SerializeUtil.serialize(person));
person = new Person(101, "bruce");
jedis.set("person:101".getBytes(), SerializeUtil.serialize(person));
}

运行上面代码之后,我们到命令行窗口中读取该对象,看看有没有写入成功:

  1. redis 127.0.0.1:6379> get person:100  
  2. ”\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”  
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"

可以取到序列化之后的值。

4取对象
用Jedis获取对象:

  1. public Person getObject(int id) {  
  2. byte[] person = jedis.get((“person:” + id).getBytes());  
  3. return (Person) SerializeUtil.unserialize(person);  
  4. }  
public Person getObject(int id) {
byte[] person = jedis.get(("person:" + id).getBytes());
return (Person) SerializeUtil.unserialize(person);
}

测试一下上一步存入的两个对象:

  1. Person person = test.getObject(100);  
  2. System.out.println(person.getId());  
  3. System.out.println(person.getName());  
  4. person = test.getObject(101);  
  5. System.out.println(person.getId());  
  6. System.out.println(person.getName());  
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控制台输入:

  1. 100  
  2. alan  
  3. 101  
  4. bruce  
100
alan
101
bruce

由此可见,序列化对象在Redis中存取正确。



转自【http://alanland.iteye.com/admin/blogs/1600685】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值