package com.gametech.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.apache.log4j.Logger; public class SerializeUtil { static Logger log = Logger.getLogger(SerializeUtil.class); /** * * <p>Title: ObjTOSerialize</p> * <p>Description: 序列化一个对象</p> * @param obj * @return * @author guangshuai.wang */ public static byte[] ObjTOSerialize(Object obj){ ObjectOutputStream oos = null; ByteArrayOutputStream byteOut = null; try{ byteOut = new ByteArrayOutputStream(); oos = new ObjectOutputStream(byteOut); oos.writeObject(obj); byte[] bytes = byteOut.toByteArray(); return bytes; }catch (Exception e) { log.error("对象序列化失败"); } return null; } /** * * <p>Title: unSerialize</p> * <p>Description: 反序列化</p> * @param bytes * @return * @author guangshuai.wang */ public static Object unSerialize(byte[] bytes){ ByteArrayInputStream in = null; try{ in = new ByteArrayInputStream(bytes); ObjectInputStream objIn = new ObjectInputStream(in); return objIn.readObject(); }catch (Exception e) { log.error("反序列化失败"); } return null; } } 2.使用jedis.set(byte[], byte[])通过这个API,很显然我们能够实现jedis.set(String key, Object value)jedis.set(String key, List<M> values)我们需要关注的就是在cache的时候将Object和List对象转换成字节数组并且需要提供将字节数组转换成对象返回。如果需要取对象的时候使用jedis.get:
新闻热点
疑难解答