一、到目前为止(jedis-2.2.0.jar),在Jedis中其实并没有提供这样的API对对象,或者是List对象的直接缓存,即并没有如下类似的API
jedis.set(String key, Object value)
jedis.set(String key, List
而更多的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
我们需要关注的就是在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 deserialize(byte[] in) { List 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 values = (List) 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 {