public class SerializeUtil { // 序列化 public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; byte[] bytes = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); bytes = baos.toByteArray(); } catch (Exception e) { System.err.println("序列化失败" + e.getMessage()); } return bytes; } // 反序列化 public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException { ByteArrayInputStream bais = null; ObjectInputStream ois = null; try { bais = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bais); } catch (Exception e) { System.err.println("反序列化失败" + e.getMessage()); } return ois.readObject(); } }
String id=map.get("id").toString();
System.out.println(Thread.currentThread().getName());
Jedis jedis = TaskAppThread.getJedis("default");
if(jedis.get(id.getBytes())!=null){
byte[] bytes = jedis.get(id.getBytes());
try {
Object deserialize = SerializeUtil.deserialize(bytes);
JSONObject objJson = JSONObject.fromObject(deserialize);
StationInfo stationInfo = (StationInfo) JSONObject.toBean(objJson,StationInfo.class);
return stationInfo;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Long setnx = jedis.setnx(id, "-1");
if(setnx==-1){
return null;
}
StationInfo getinfo = chDao.getinfo(id);
byte[] result = SerializeUtil.serialize(getinfo);
jedis.setex(id.getBytes(),100,result);
原文:https://www.cnblogs.com/mengziyang/p/14846348.html