一、Kryo
写入文件:
public static <T> boolean serializeFile(T obj,String path) {
if(null != obj){
Output output=null;
try {
File outFile = new File(path);
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
Kryo kryo = kryos.get();
output = new Output(new FileOutputStream(path));
kryo.writeObject(output, obj);
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
close(output);
}
}
return false;
}
读取文件:
public static <T> T unSerializeFile(String path,Class<T> t) {
if(null != path && null !=t ){
Input input=null;
try {
Kryo kryo = kryos.get();
input = new Input(new FileInputStream(path));
return kryo.readObject(input,t);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(input);
}
}
return null;
}
二、内存快照
写入:
public static void takeSnapshot(String cachePath, String cacheFileName, PageCacheVO space) {
ObjectOutputStream oos = null;
try {
File dir = new File(cachePath);
if (!dir.exists()) {
boolean mkdir = dir.mkdirs();
if(!mkdir){
throw new RuntimeException("Failed to create directory!");
}
}
File file = new File(cachePath,cacheFileName);
FileOutputStream fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(space);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
读取:
public static PageCacheVO readSnapshot(String cachePath) {
try {
File file = new File(cachePath);
long startTime = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(file);
startTime = System.currentTimeMillis();
ObjectInputStream ois = new ObjectInputStream(fis);
startTime = System.currentTimeMillis();
PageCacheVO space = (PageCacheVO) ois.readObject();
ois.close();
return space;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
三、内存快照 + GZIP
写入:
public static void takeSnapshotGzip(String cachePath, String cacheFileName, PageCacheVO space) {
ObjectOutputStream oos = null;
try {
File dir = new File(cachePath);
if (!dir.exists()) {
boolean mkdir = dir.mkdirs();
if(!mkdir){
throw new RuntimeException("Failed to create directory!");
}
}
File file = new File(cachePath,cacheFileName);
// FileOutputStream fos = new FileOutputStream(file);
FileOutputStream fos = new FileOutputStream(file);
GZIPOutputStream gz = new GZIPOutputStream(fos);
oos = new ObjectOutputStream(gz);
oos.writeObject(space);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(oos!=null){
oos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
读取:
public static PageCacheVO readSnapshotGzip(String cachePath) {
try {
File file = new File(cachePath);
long startTime = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(file);
startTime = System.currentTimeMillis();
// ObjectInputStream ois = new ObjectInputStream(fis);
GZIPInputStream gis = new GZIPInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(gis);
startTime = System.currentTimeMillis();
PageCacheVO space = (PageCacheVO) ois.readObject();
ois.close();
return space;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
执行测试的代码:
public static void main(String[] args) {
String name="张三";
PageCacheVO pageCacheVO = new PageCacheVO();
pageCacheVO.setBatchNumber(9527);
Long startTime = System.currentTimeMillis();
KryoUtil.serializeFile(pageCacheVO, "F:\\test\\kryo.cache");
System.out.println("Kryo-存储序列化文件耗时:"+(System.currentTimeMillis()-startTime) );
PageCacheVO reader1 = KryoUtil.unSerializeFile("F:\\test\\kryo.cache", PageCacheVO.class);
startTime = System.currentTimeMillis();
System.out.println("Kryo-读取序列化文件耗时:"+(System.currentTimeMillis()-startTime)+ " 解析出文件内容:"+reader1.getBatchNumber());
startTime = System.currentTimeMillis();
KryoUtil.takeSnapshot("F:\\test", "kryo2.cache",pageCacheVO);
System.out.println("内存快照-存储序列化文件耗时:"+(System.currentTimeMillis()-startTime));
startTime = System.currentTimeMillis();
PageCacheVO reader2 = KryoUtil.readSnapshot("F:\\test\\kryo2.cache");
System.out.println("内存快照-读取序列化文件耗时:"+(System.currentTimeMillis()-startTime)+ " 解析出文件内容:"+reader2.getBatchNumber());
startTime = System.currentTimeMillis();
KryoUtil.takeSnapshotGzip("F:\\test", "kryo3.cache",pageCacheVO);
System.out.println("GZIP内存快照-存储序列化文件耗时:"+(System.currentTimeMillis()-startTime));
startTime = System.currentTimeMillis();
PageCacheVO reader3 = KryoUtil.readSnapshotGzip("F:\\test\\kryo3.cache");
System.out.println("GZIP内存快照-读取序列化文件耗时:"+(System.currentTimeMillis()-startTime)+ " 解析出文件内容:"+reader3.getBatchNumber());
}
执行结果: