序列化緩存
* 用途是先將對象序列化成2進制,再緩存,好處是將對象壓縮了,省內存
* 壞處是速度慢了
private byte[] serialize(Serializable value) { try { //序列化核心就是ByteArrayOutputStream ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(value); oos.flush(); oos.close(); return bos.toByteArray(); } catch (Exception e) { throw new CacheException("Error serializing object. Cause: " + e, e); } }
private Serializable deserialize(byte[] value) { Serializable result; try { //反序列化核心就是ByteArrayInputStream ByteArrayInputStream bis = new ByteArrayInputStream(value); ObjectInputStream ois = new CustomObjectInputStream(bis); result = (Serializable) ois.readObject(); ois.close(); } catch (Exception e) { throw new CacheException("Error deserializing object. Cause: " + e, e); } return result; }