截止到目前为止,在redis官方的文档和实现里面并没有针对object 对象缓存的方法,然而,在我们的实际开发需要中,在很多时候我们是需要进行对象缓存的,并且可以正确的读取出来! 在笔者正在开发的红包项目中,针对每天红包就需要使用的对象缓存,并可以随时修改缓存对象中的红包数量值等信息!那么具体实现呢?
在官方提供的方法中,我们找到了有这么一个操作方法:
jedis.set(byte[], byte[])
看这个方法,是进行字节码操作的,这让我们很容易想到在一些远程方法调用中,我们传递对象同样传递的是字节码,是不是可以参考呢?
首先,既然需要对对象进行字节操作,即可写和可读的操作,为了保证这个原则,那么缓存对象需要实现Serializable 接口,进行序列化和反序列化!
涉及到的知识点:
1: Serializable (接口,实现此接口的对象可以进行序列化)
2: ByteArrayOutputStream,ObjectOutputStream 对象转换为字节码输出流
3: ByteArrayInputStream ,ObjectInputStream 字节码转换为对象的输入流
了解了如上三点知识后,我们就可以对对象进行缓存操作了!
示例代码如下,包括了对象缓存和List 对象数组缓存,需要声明的是,放入list数组中的对象同样需要实现Serializable 接口:
public class ObjectsTranscoder extends SerializeTranscoder { @SuppressWarnings("unchecked") @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; } @SuppressWarnings("unchecked") @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 { @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) { 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 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); } return results; } }
通过以上操作即可以实现对象的缓存和读取了!
虽然自己实现了,还是希望redis官方尽快提供对象的缓存操作吧!