一.序列化工具类(因为Jedis的键是区分String,byte类型的,所以使用序列化成byte,再使用反序列化显示)
package com.yk;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerialoizebleUtil {
/**
* 将对象序列化:要求对象要实现序列化接口
*/
public static byte[] serialize(Object obj ){
ObjectOutputStream oos = null; //对象输出流
ByteArrayOutputStream baos = null;//字节数组输出流
byte[] bt=null;
try {
baos = new ByteArrayOutputStream();
oos=new ObjectOutputStream(baos);
oos.writeObject(obj);
bt=baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bt;
}
//反序列化,将byte字节转换为对象
public static Object unSerialize(byte[] bt){
ByteArrayInputStream bais = null;
Object object =null;
try {
bais = new ByteArrayInputStream(bt);
ObjectInputStream ois = new ObjectInputStream(bais);
object = ois .readObject();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(bais!=null){
try {
bais.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return object;
}
}
二.redis连接池类(读取配置文件,创建redis连接池,获取redis连接)
package com.yk;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ResourceBundle;
public class RedisPool {
private static JedisPool pool =null;
/*使用静态代码块,优先加载顺序在static方法之前
* 初始化redis连接配置
*/
static {
//得到redis.properties 中的配置信息
ResourceBundle bundle = ResourceBundle.getBundle("redis");