Redis学习笔记二

学习Redis添加Object时,由于Redis只能存取字符串String,对于其它数据类型形容:Int,long,double,Date等不提供支持,因而需要设计到对象的序列化和反序列化.java序列化的过程就是将对象转变为byte,字节码的过程. Java的反序列过程就是就是将字节码恢复成对象的过程。

一正一逆,也迎合了电脑存储数据的特点。

关于Java的序列化和反序列化的Demo:

 package hbut.base;

 import org.junit.Test;

 import java.io.*;

 /**
* @Author XiJun.Gong
* @DATE 2016/6/12.
* aim: hbut.base
* function: 对象的序列化和反序列
*/
public class serializableDemo { private User user = null;
private ByteArrayOutputStream byteArrayOutputStream = null;
private ObjectOutputStream outputStream = null;
private ObjectInputStream inputStream = null;
private ByteArrayInputStream byteArrayInputStream = null;
private byte[] result; @Test
public void test() {
user = new User();
user.setUsername("huifeidmeng");
user.setAge(); try {
byteArrayOutputStream = new ByteArrayOutputStream();
outputStream = new ObjectOutputStream(byteArrayOutputStream);
outputStream.writeObject(user);
result = byteArrayOutputStream.toByteArray();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (outputStream != null) outputStream.close();
if (byteArrayOutputStream != null) byteArrayOutputStream.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
/*tmp sout*/
System.out.println(result);
User tmpUser = new User();
try {
byteArrayInputStream = new ByteArrayInputStream(result);
inputStream = new ObjectInputStream(byteArrayInputStream);
tmpUser = (User) inputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e3) {
e3.printStackTrace();
} finally {
try {
if (byteArrayInputStream != null) byteArrayInputStream.close();
if (inputStream != null) inputStream.close();
} catch (IOException e4) {
e4.printStackTrace();
} }
System.out.println(tmpUser.getUsername() + ": " + tmpUser.getAge());
} } class User implements Serializable {
private static final long serialVersionUID = -1941046831377985500L;
String username;
Integer age; public User() {
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

在编写demo的过程中,发现一个问题,当使用内部类作为对象的的时候,序列化总是抛出异常,但是一旦将User类迁出的时候,又恢复正常。

关于序列化和反序列工具类Dmeo:

 package com.hbut.util;

 import java.io.*;

 /**
* @Author XiJun.Gong
* @DATE 2016/6/13.
* aim: com.hbut.util
*/
public class SerializableTool { private ByteArrayOutputStream byteArrayOutputStream = null;
private ObjectOutputStream outputStream = null;
private ObjectInputStream inputStream = null;
private ByteArrayInputStream byteArrayInputStream = null; public byte[] serializable(Object object) {
byte[] result = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
outputStream = new ObjectOutputStream(byteArrayOutputStream);
outputStream.writeObject(object);
result = byteArrayOutputStream.toByteArray();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (outputStream != null) outputStream.close();
if (byteArrayOutputStream != null) byteArrayOutputStream.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
return result;
} public Object unSerializable(byte[] bytes) {
Object object = null;
try {
byteArrayInputStream = new ByteArrayInputStream(bytes);
inputStream = new ObjectInputStream(byteArrayInputStream);
object = inputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e3) {
e3.printStackTrace();
} finally {
try {
if (byteArrayInputStream != null) byteArrayInputStream.close();
if (inputStream != null) inputStream.close();
} catch (IOException e4) {
e4.printStackTrace();
}
}
return object;
}
}

Redis存取对象和读取对象Demo

 class UserModel implements Serializable {

     private String username;
private String password;
private Integer age; @Override
public String toString() {
return "UserModel{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
} public UserModel() { } public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} 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;
}
}

该是这部分的继续:

启动Redis服务器,后运行如下代码:

 package com.hbut.util;

 import com.google.common.collect.Maps;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map; /**
* Created by XiJun.Gong on 14-2-28.
*/
public class TestRedis { JedisPool pool;
Jedis jedis; /**
* connection
*/
@Before
public void init() {
pool = new JedisPool(new JedisPoolConfig(), "localhost");
jedis = pool.getResource();
//jedis.auth("*******"); //密码验证
} /**
* for object*
*/
@Test
public void testObject() { SerializableTool serializableTool = new SerializableTool();
UserModel userModel = new UserModel();
userModel.setAge(123);
userModel.setUsername("huifeidmeng");
userModel.setPassword("******");
byte[] bytes = serializableTool.serializable(userModel);
jedis.set("useModel".getBytes(), bytes);
byte[] result = jedis.get("useModel".getBytes());
Object object = serializableTool.unSerializable(result);
UserModel tmpUser = (UserModel) object;
System.out.println(tmpUser.toString());
} }

运行结果:

UserModel{username='huifeidmeng', password='******', age=123}

上一篇:redis 学习笔记二


下一篇:Swoft2.x 小白学习笔记 (二) --- mysql、redis