一、到目前為止(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); }