截止到目前为止,在redis官方的文档和实现里面并没有针对object 对象缓存的方法,然而,在我们的实际开发需要中,在很多时候我们是需要进行对象缓存的,并且可以正确的读取出来! 在笔者正在开发的红包项目中,针对每天红包就需要使用的对象缓存,并可以随时修改缓存对象中的红包数量值等信息!那么具体实现呢?
在官方提供的方法中,我们找到了有这么一个操作方法:
jedis.set(byte[], byte[])
看这个方法,是进行字节码操作的,这让我们很容易想到在一些远程方法调用中,我们传递对象同样传递的是字节码,是不是可以参考呢?
首先,既然需要对对象进行字节操作,即可写和可读的操作,为了保证这个原则,那么缓存对象需要实现Serializable 接口,进行序列化和反序列化!
涉及到的知识点:
1: Serializable (接口,实现此接口的对象可以进行序列化)
2: ByteArrayOutputStream,ObjectOutputStream 对象转换为字节码输出流
3: ByteArrayInputStream ,ObjectInputStream 字节码转换为对象的输入流
了解了如上三点知识后,我们就可以对对象进行缓存操作了!
示例代码如下,包括了对象缓存和List 对象数组缓存,需要声明的是,放入list数组中的对象同样需要实现Serializable 接口:
public class ObjectsTranscoder extends SerializeTranscoder {<span style="color:#808000;">@SuppressWarnings</span>(<span style="color:#008000;"><strong>"unchecked"</strong></span>) <span style="color:#808000;">@Override
public byte[] serialize(Object value) {
if (value == null) {
throw new NullPointerException(“Can’t serialize null”);
}
byte[] result = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
os.writeObject(value);
os.close();
bos.close();
result = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException(“Non-serializable object”, e);
} finally {
close(os);
close(bos);
}
return result;
}
<span style="color:#808000;">@SuppressWarnings</span>(<span style="color:#008000;"><strong>"unchecked"</strong></span>)
<span style="color:#808000;">@Override
public Object deserialize(byte[] in) {
Object result = null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
result = is.readObject();
is.close();
bis.close();
}
} catch (IOException e) {
logger.error(String.format(“Caught IOException decoding %d bytes of data”,
in == null ? 0 : in.length) + e);
} catch (ClassNotFoundException e) {
logger.error(String.format(“Caught CNFE decoding %d bytes of data”,
in == null ? 0 : in.length) + e);
} finally {
close(is);
close(bis);
}
return result;
}
}
对象的转换示例如上代码:
数组缓存代码:
public class ListTranscoder<M extends Serializable> extends SerializeTranscoder {<span style="color:#808000;">@SuppressWarnings</span>(<span style="color:#008000;"><strong>"unchecked"</strong></span>) <span style="color:#000080;"><strong>public </strong></span>List<<span style="color:#20999d;">M</span>> deserialize(<span style="color:#000080;"><strong>byte</strong></span>[] in) { List<<span style="color:#20999d;">M</span>> list = <span style="color:#000080;"><strong>new </strong></span>ArrayList<>(); ByteArrayInputStream bis = <span style="color:#000080;"><strong>null</strong></span>; ObjectInputStream is = <span style="color:#000080;"><strong>null</strong></span>; <span style="color:#000080;"><strong>try </strong></span>{ <span style="color:#000080;"><strong>if </strong></span>(in != <span style="color:#000080;"><strong>null</strong></span>) { bis = <span style="color:#000080;"><strong>new </strong></span>ByteArrayInputStream(in); is = <span style="color:#000080;"><strong>new </strong></span>ObjectInputStream(bis); <span style="color:#000080;"><strong>while </strong></span>(<span style="color:#000080;"><strong>true</strong></span>) { <span style="color:#20999d;">M </span>m = (<span style="color:#20999d;">M</span>)is.readObject(); <span style="color:#000080;"><strong>if </strong></span>(m == <span style="color:#000080;"><strong>null</strong></span>) { <span style="color:#000080;"><strong>break</strong></span>; } list.add(m); } is.close(); bis.close(); } } <span style="color:#000080;"><strong>catch </strong></span>(IOException e) { <span style="color:#660e7a;"><em>logger</em></span>.error(String.<span style="font-style:italic;">format</span>(<span style="color:#008000;"><strong>"Caught IOException decoding %d bytes of data"</strong></span>, in == <span style="color:#000080;"><strong>null </strong></span>? <span style="color:#0000ff;">0 </span>: in.<span style="color:#660e7a;"><strong>length</strong></span>) + e); } <span style="color:#000080;"><strong>catch </strong></span>(ClassNotFoundException e) { <span style="color:#660e7a;"><em>logger</em></span>.error(String.<span style="font-style:italic;">format</span>(<span style="color:#008000;"><strong>"Caught CNFE decoding %d bytes of data"</strong></span>, in == <span style="color:#000080;"><strong>null </strong></span>? <span style="color:#0000ff;">0 </span>: in.<span style="color:#660e7a;"><strong>length</strong></span>) + e); } <span style="color:#000080;"><strong>finally </strong></span>{ close(is); close(bis); } <span style="color:#000080;"><strong>return </strong></span>list; } <span style="color:#808000;">@SuppressWarnings</span>(<span style="color:#008000;"><strong>"unchecked"</strong></span>) <span style="color:#808000;">@Override
public byte[] serialize(Object value) {
if (value == null)
throw new NullPointerException(“Can’t serialize null”);
List<<span style="color:#20999d;">M</span>> values = (List<<span style="color:#20999d;">M</span>>) value;
<span style="color:#000080;"><strong>byte</strong></span>[] results = <span style="color:#000080;"><strong>null</strong></span>;
ByteArrayOutputStream bos = <span style="color:#000080;"><strong>null</strong></span>;
ObjectOutputStream os = <span style="color:#000080;"><strong>null</strong></span>;
<span style="color:#000080;"><strong>try </strong></span>{
bos = <span style="color:#000080;"><strong>new </strong></span>ByteArrayOutputStream();
os = <span style="color:#000080;"><strong>new </strong></span>ObjectOutputStream(bos);
<span style="color:#000080;"><strong>for </strong></span>(<span style="color:#20999d;">M </span>m : values) {
os.writeObject(m);
}
<span style="color:#808080;"><em>// 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);
}
<span style="color:#000080;"><strong>return </strong></span>results;
}
}
通过以上操作即可以实现对象的缓存和读取了!
虽然自己实现了,还是希望redis官方尽快提供对象的缓存操作吧!