Redis 存储List对象

  1. rn rv;  
  2.         }  
  3.   
  4.         public static List<User> deserialize(byte[] in) {  
  5.             List<User> list = new ArrayList<User>();  
  6.             ByteArrayInputStream bis = null;  
  7.             ObjectInputStream is = null;  
  8.             try {  
  9.                 if(in != null) {  
  10.                     bis=new ByteArrayInputStream(in);  
  11.                     is=new ObjectInputStream(bis);  
  12.                     while (true) {  
  13.                         User user = (User) is.readObject();  
  14.                         if(user == null){  
  15.                             break;  
  16.                         }else{  
  17.                             list.add(user);  
  18.                         }  
  19.                     }  
  20.                     is.close();  
  21.                     bis.close();  
  22.                 }  
  23.             } catch (IOException e) {  
  24.                 logger.warn("Caught IOException decoding %d bytes of data",  
  25.                         in == null ? 0 : in.length, e);  
  26.             } catch (ClassNotFoundException e) {  
  27.                 logger.warn("Caught CNFE decoding %d bytes of data",  
  28.                         in == null ? 0 : in.length, e);  
  29.             } finally {  
  30.                 CloseUtil.close(is);  
  31.                 CloseUtil.close(bis);  
  32.             }  
  33.             return list;  
  34.         }  
  35.     }  
  36.       
  37.     static class ListTranscoder{  
  38.         public static byte[] serialize(Object value) {  
  39.             if (value == null) {  
  40.                 throw new NullPointerException("Can't serialize null");  
  41.             }  
  42.             byte[] rv=null;  
  43.             ByteArrayOutputStream bos = null;  
  44.             ObjectOutputStream os = null;  
  45.             try {  
  46.                 bos = new ByteArrayOutputStream();  
  47.                 os = new ObjectOutputStream(bos);  
  48.                 os.writeObject(value);  
  49.                 os.close();  
  50.                 bos.close();  
  51.                 rv = bos.toByteArray();  
  52.             } catch (IOException e) {  
  53.                 throw new IllegalArgumentException("Non-serializable object", e);  
  54.             } finally {  
  55.                 close(os);  
  56.                 close(bos);  
  57.             }  
  58.             return rv;  
  59.         }  
  60.   
  61.         public static Object deserialize(byte[] in) {  
  62.             Object rv=null;  
  63.             ByteArrayInputStream bis = null;  
  64.             ObjectInputStream is = null;  
  65.             try {  
  66.                 if(in != null) {  
  67.                     bis=new ByteArrayInputStream(in);  
  68.                     is=new ObjectInputStream(bis);  
  69.                     rv=is.readObject();  
  70.                     is.close();  
  71.                     bis.close();  
  72.                 }  
  73.             } catch (IOException e) {  
  74.                 logger.warn("Caught IOException decoding %d bytes of data",  
  75.                         in == null ? 0 : in.length, e);  
  76.             } catch (ClassNotFoundException e) {  
  77.                 logger.warn("Caught CNFE decoding %d bytes of data",  
  78.                         in == null ? 0 : in.length, e);  
  79.             } finally {  
  80.                 CloseUtil.close(is);  
  81.                 CloseUtil.close(bis);  
  82.             }  
  83.             return rv;  
  84.         }  
  85.     }  
  86. }  

PSRedsi中存储list没有封装对ObjectAPI,是不是也是倾向于只存储用到的字段,而不是存储Object本身呢?Redis是一个In-Mem的产品,会觉得我们应用的方式。


http://blog.csdn.net/gstormspire/article/details/7653095


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值