Redis缓存Object,List对象

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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@

*/

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) {

("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@

*/

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);

}

相关文档
最新文档