Redis缓存Object,List对象

一、到目前为止(jedis-2.2.0.jar),在Jedis中其实并没有提供这样的API对对象,或者是List对象的直接缓存,即并没有如下类似的API

jedis.set(String key, Object value)

jedis.set(String key, List<M> values)

而更多的API是类似于 jedis.set(String key, String value)或者jedis.set(String key, String ... value)

那如果我们想缓存对象,怎么办呢?

二、通过研究Jedis的API,我们发现其提供了这样的API

jedis.set(byte[], byte[]),

通过这个API,很显然我们能够实现

jedis.set(String key, Object value)

jedis.set(String key, List<M> values)

我们需要关注的就是在cache的时候将Object和List对象转换成字节数组并且需要提供将字节数组转换成对象返回。

三、考虑到扩展性,设计了3个类,抽象父类SerializeTranscoder, 子类ListTranscoder和ObjectsTranscoder 以及一个Unit test 类 用于模拟对象,list对象和字节数组的转换,即Serialize和de-searilize的过程。

1. SerializeTranscoder

package com.chuanliu.platform.activity.basic.util.serialize;

import java.io.Closeable;

import org.apache.log4j.Logger;


/**
 * @author Josh Wang(Sheng) * * @email josh_wang23@hotmail.com */ public abstract class SerializeTranscoder {  protected static Logger logger = Logger.getLogger(SerializeTranscoder.class);   public abstract byte[] serialize(Object value);   public abstract Object deserialize(byte[] in);   public void close(Closeable closeable) {   if (closeable != null) {    try {     closeable.close();    } catch (Exception e) {     logger.info("Unable to close " + closeable, e);    }   }  } }

2. ListTranscoder

package com.chuanliu.platform.activity.basic.util.serialize;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import com.chuanliu.platform.activity.basic.util.LoggerUtils; /** * Case 1. * Jedis does not support cache the Object directly, the Objects needed to be * serialized and de-serialized * * Case 2. * coming very soon... * * @author Josh Wang(Sheng) * * @email josh_wang23@hotmail.com */ public class ListTranscoder<M extends Serializable> extends SerializeTranscoder {   @SuppressWarnings("unchecked")  public List<M> deserialize(byte[] in) {   List<M> list = new ArrayList<>();   ByteArrayInputStream bis = null;   ObjectInputStream is = null;   try {    if (in != null) {     bis = new ByteArrayInputStream(in);     is = new ObjectInputStream(bis);     while (true) {      M m = (M)is.readObject();      if (m == null) {       break;      }           list.add(m);          }     is.close();     bis.close();    }   } catch (IOException e) {  LoggerUtils.error(logger, String.format("Caught IOException decoding %d bytes of data",   in == null ? 0 : in.length) + e);  } catch (ClassNotFoundException e) {  LoggerUtils.error(logger, String.format("Caught CNFE decoding %d bytes of data",   in == null ? 0 : in.length) + e);  } finally {    close(is);    close(bis);   }     return list;  }   @SuppressWarnings("unchecked")  @Override  public byte[] serialize(Object value) {   if (value == null)    throw new NullPointerException("Can't serialize null");     List<M> values = (List<M>) value;     byte[] results = null;   ByteArrayOutputStream bos = null;   ObjectOutputStream os = null;     try {    bos = new ByteArrayOutputStream();    os = new ObjectOutputStream(bos);    for (M m : values) {     os.writeObject(m);    }       // os.writeObject(null);    os.close();    bos.close();    results = bos.toByteArray();   } catch (IOException e) {    throw new IllegalArgumentException("Non-serializable object", e);   } finally {    close(os);    close(bos);   } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值