原文地址:https://www.i847.cn/article/8.html
RedisTemplate序列化工具效率对比:
1.JdkSerializationRedisSerializer
2.GenericJackson2JsonRedisSerializer
3.Jackson2JsonRedisSerializer
测试代码片段:
@Test
public void test7(){
Page page = sysMessageEventDao.findAll(Pages.Helper.pageable(1, 200, null));
List list = page.getContent();
JdkSerializationRedisSerializer j = new JdkSerializationRedisSerializer();
GenericJackson2JsonRedisSerializer g = new GenericJackson2JsonRedisSerializer();
Jackson2JsonRedisSerializer j2 = new Jackson2JsonRedisSerializer(List.class);
TimeTest tt = new TimeTest();
tt.setStartTime();
byte[] bytesJ = j.serialize(list);
tt.setEndTime();
System.out.println("JdkSerializationRedisSerializer序列化时间:"+tt.getExcuteTime() + "ms,序列化后的长度:" + bytesJ.length);
tt.setStartTime();
j.deserialize(bytesJ);
tt.setEndTime();
System.out.println("JdkSerializationRedisSerializer反序列化时间:"+tt.getExcuteTime());
tt.setStartTime();
byte[] bytesG = g.serialize(list);
tt.setEndTime();
System.out.println("GenericJackson2JsonRedisSerializer序列化时间:"+tt.getExcuteTime() + "ms,序列化后的长度:" + bytesG.length);
tt.setStartTime();
g.deserialize(bytesG);
tt.setEndTime();
System.out.println("GenericJackson2JsonRedisSerializer反序列化时间:"+tt.getExcuteTime());
tt.setStartTime();
byte[] bytesJ2 = j2.serialize(list);
tt.setEndTime();
System.out.println("Jackson2JsonRedisSerializer序列化时间:"+tt.getExcuteTime() + "ms,序列化后的长度:" + bytesJ2.length);
tt.setStartTime();
j2.deserialize(bytesJ2);
tt.setEndTime();
System.out.println("Jackson2JsonRedisSerializer反序列化时间:"+tt.getExcuteTime());
}
测试数据(总共200条记录的List集合),单条类似:
{"channel":"USER_LOGIN@30-9C-23-83-D0-9C","createTime":1526973952000,"doneHost":"192.168.80.10:62016","doneServer":"he-pm","doneTime":1526973952000,"eventInfoJson":"{\"userName\":\"Mos.Jiang\",\"targetId\":\"250524c0-8869-4fc2-a4aa-278a5a5b3197\",\"localId\":\"250524c0-8869-4fc2-a4aa-278a5a5b3197\",\"first\":\"您好,您账号已经登录\",\"keyword1\":\"2018年05月22日 15:25\",\"keyword2\":\"127.0.0.1\",\"remark\":\"如果本次登录不是您本人所为,说明您的帐号已经被盗!为减少您的损失,请立即联系客服 4008271580。\"}","hasDone":1,"id":"006bdac0-0ba0-498d-b654-28fa6038f747","sendCount":1,"sendTime":1526973952000,"senderInfo":"com.mj.he800.pm.service.user.UserBaseInfoService#userLogin-215"}
测试结果:
JdkSerializationRedisSerializer序列化时间:11,序列化后的长度:94551
JdkSerializationRedisSerializer反序列化时间:15
GenericJackson2JsonRedisSerializer序列化时间:18,序列化后的长度:151108
GenericJackson2JsonRedisSerializer反序列化时间:14
Jackson2JsonRedisSerializer序列化时间:5,序列化后的长度:125489
Jackson2JsonRedisSerializer反序列化时间:3
简单地可以看出,Jackson2JsonRedisSerializer序列化和反序列化效率高,JdkSerializationRedisSerializer序列化后的结果最短