有两种方法可以实现存储对象和泛型
1.用序列化和反序列化
2.json
序列化工具类,实现序列化和反序列话对象和list集合
package com; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; /** * 序列化工具类 * @author caspar * */ public class SerializeUtil { /** * 序列化 * @param object * @return */ public static byte[] serialize(Object object) { if (object == null) { return null; } ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; byte[] bytes = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); bytes = baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { close(oos); close(baos); } return bytes; } /** * 反序列化 * * @param bytes * @return */ public static Object unserialize(byte[] bytes) { if (bytes == null) { return null; } ByteArrayInputStream bais = null; ObjectInputStream ois = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { e.printStackTrace(); } finally { close(bais); close(ois); } return null; } /** * 序列化 list 集合 * * @param list * @return */ public static byte[] serializeList(List<?> list) { if (CommonUtil.isEmptyList(list)) { return null; } ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; byte[] bytes = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); for (Object obj : list) { oos.writeObject(obj); } bytes = baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { close(oos); close(baos); } return bytes; } /** * 反序列化 list 集合 * * @param lb * @return */ public static List<?> unserializeList(byte[] bytes) { if (bytes == null) { return null; } List<Object> list = new ArrayList<Object>(); ByteArrayInputStream bais = null; ObjectInputStream ois = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bais); while (bais.available() > 0) { Object obj = (Object) ois.readObject(); if (obj == null) { break; } list.add(obj); } } catch (Exception e) { e.printStackTrace(); } finally { close(bais); close(ois); } return list; } /** * 关闭io流对象 * * @param closeable */ public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { e.printStackTrace(); } } } }
/** * 设置对象 * @param key * @param obj */ public static void setObject(String key ,Object obj){ try { obj = obj == null ? new Object():obj; getJedis().set(key.getBytes(), SerializeUtil.serialize(obj)); } catch (Exception e) { e.printStackTrace(); } } /** * 获取对象 * @param key * @return Object */ public static Object getObject(String key){ if(getJedis() == null || !getJedis().exists(key)){ return null; } byte[] data = getJedis().get(key.getBytes()); return (Object)SerializeUtil.unserialize(data); } /** * 设置List集合 * @param key * @param list */ public static void setList(String key ,List<?> list){ try { if(CommonUtil.isNotEmptyList(list)){ getJedis().set(key.getBytes(), SerializeUtil.serializeList(list)); }else{//如果list为空,则设置一个空 getJedis().set(key.getBytes(), "".getBytes()); } } catch (Exception e) { e.printStackTrace(); } } /** * 获取List集合 * @param key * @return */ public static List<?> getList(String key){ if(getJedis() == null || !getJedis().exists(key)){ return null; } byte[] data = getJedis().get(key.getBytes()); return SerializeUtil.unserializeList(data); }
public static void main(String[] args) { //object setObject("100",new Person("caspar",25)); Person p = (Person)getObject("100"); System.out.println(p.getName()+"----"+p.getAge()); //list List<Person> list = new ArrayList<Person>(); list.add(new Person("唐马儒",39)); list.add(new Person("大便熊",33)); list.add(new Person("小萝莉",14)); setList("list001", list); List<Person> resultList = (List<Person>) getList("list001"); for (Person person : resultList) { System.out.println(person.getName()+"----"+person.getAge()); } }
caspar----25
唐马儒----39
大便熊----33
小萝莉----14
正常情况下效率也挺高,但是如果再高并发的情况下,序列化和反序列化消耗太多,redis不支持存储object和泛型,是有理由的。
建议使用json来存储
把object和list<?> 转成json的字符串格式再set到redis里面,取得时候再把json转换为需要的对象,这样简单快捷,推荐使用
原文:http://blog.csdn.net/tuposky/article/details/45339863