记录一下:

先粘两个比较繁琐的方法:

put:

public void putSerializableObject(String key, Object value, int expireTime) {
key = preProcessKey(key);
ByteArrayOutputStream byteArrayOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(value);
byte[] bytes = byteArrayOutputStream.toByteArray();
cluster.add(key, expireTime, bytes);
} catch (Exception e) {
logger.warn("Memcache put() failed! key=" + key, e);
} finally {
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
logger.warn("ByteArrayOutputStream close() failed! key=" + key + e);
}
}
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (IOException e) {
logger.warn("ObjectOutputStream close() failed! key=" + key + e);
}
}
}
}

get:

public <T> T getSerializableObject(String key, Class<T> clazz) {
key = preProcessKey(key);
T result = null;
ByteArrayInputStream byteArrayInputStream = null;
ObjectInputStream objectInputStream = null;
try {
byte[] resultByte = (byte[]) cluster.get(key);
if (null != resultByte) {
byteArrayInputStream = new ByteArrayInputStream(resultByte);
objectInputStream = new ObjectInputStream(byteArrayInputStream);
result = (T) objectInputStream.readObject();
}
} catch (Exception e) {
logger.warn("Memcache get() failed! key=" + key, e);
return null;
} finally {
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (IOException e) {
logger.warn("ObjectInputStream close() failed ! key=" + key + e);
}
}
if (byteArrayInputStream != null) {
try {
byteArrayInputStream.close();
} catch (IOException e) {
logger.warn("ByteArrayInputStream close() failed ! key=" + key + e);
}
}
}
return result;
}

重点!

上面两个方法,有冗余的代码,可以进一步简化:

  序列化:

    public static byte[] serialize(Object object) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(object);
return baos.toByteArray();
}
}

  反序列化:

    public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try (ObjectInputStream ois = new ObjectInputStream(bais)) {
return ois.readObject();
}
}

由于 InputStream继承了Closeable,当在try-cache中使用流的时候,会在执行结束try-cache后自动调用close方法,无论是否抛出异常,代码简洁多了,很棒棒哦 (*^▽^*)~

最新文章

  1. fir.im Weekly - APP 性能监测优化 二三事
  2. Centos 上 Tengine安装
  3. 怎样解决Myeclipse中运行jsp乱码问题,亲测有效(虽然是个小问题但是为了大家不被网络上的一些乱七八糟的回答坑)不是改什么windows-propories-...............
  4. KEIL MDK 5.12帮你快速建工程模板的技巧
  5. UVALive 4452 The Ministers&#39; Major Mess(2-sat)
  6. Xml通用操作类
  7. Oracle查询经典
  8. 生成N个不重复的随机数(转)
  9. 一个正整数N,拆成任意个正整数之和,怎样使这些数的乘积最大
  10. 同一DataTable下创建多个结构数据相同的DataView的小问题
  11. android 定时器总结
  12. Windows下编译mxnet
  13. Spring中实现文件上传
  14. Uva 10550 Combination Lock
  15. Asp.NETCore轻松学系列阅读指引目录
  16. Spark应用【根据新df更新旧df】
  17. java 得到目录路径的方法
  18. JAVA自学笔记14
  19. 学习 MeteoInfo二次开发教程(一)
  20. POJ 1741 Tree(点分治点对&lt;=k)

热门文章

  1. 使用xUnits来实现单元测试
  2. Elastic search中使用nested类型的内嵌对象
  3. JS点击img图片放大再次点击缩小JS实现 简单实用Ctrl+C+V就可以用
  4. django设置mysql为数据库笔记
  5. 【miscellaneous】北斗短报文
  6. SpringBoot项目集成cas单点登录
  7. oracle 11g sqlplus和plsql developer 乱码解决方案
  8. 2019/12.09centos安装 | 无密钥登陆
  9. Laravel中一些要记住 的写法
  10. ubuntu修改时间为北京时间