Redis存储Object 和 list<object>
来源:互联网 发布:医药b2b关键词 seo 编辑:程序博客网 时间:2024/05/21 12:41
Redis 存储支持的类型没有object ,虽然有支持list,但是只支持List<String>
redis工具类的部分方法,实现设置/获取对象和泛型值
输出结果
有两种方法可以实现存储对象和泛型
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();
- }
- }
- }
- }
redis工具类的部分方法,实现设置/获取对象和泛型值
- /**
- * 设置对象
- * @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转换为需要的对象,这样简单快捷,推荐使用
阅读全文
0 0
- Redis存储Object 和 list<object>
- Redis存储Object 和 list<object>
- Redis存储Object 和 list<object>
- Redis存储List<Map<String, Object>>
- Redis操作List<Object>
- Redis缓存Object,List对象
- List<T>和List<Object[]>
- List<object>和List<List<object>>的用法
- Redis缓存Object,List对象 - Java 对象和List的序列化和反序列化
- 存储过程的导入List<Object>集合
- List和List<Object>的区别
- Ormlite中使用Collection<Object>和Gson使用List<Object>
- List.contains(Object object)方法
- List<Object>删除某一个Object
- Dictionary 和 List<Dictionary<string, object>>
- jedis 保存自定义List和Object对象
- 返回List时List<Map>和List<Object>区别
- Redis 存储图片 [base64/url/path]vs[object]
- SparkR初体验
- Android柱状图的实现
- STL(MAP入门应用)
- JVM_类加载
- Linux系统如何查看是多少位的
- Redis存储Object 和 list<object>
- 3、js原生实现移动端手指滑动轮播图效果(3)
- python 处理Excel 常见问题- 写入Excel
- 使用fastboot命令刷机流程详解
- tar使用方法
- 2 矩阵消元
- 【编译器原理】编译器的工作过程
- java 关键字
- Codeforces 453B B. Little Pony and Harmony Chest(dp+数论)