Memcached中对象反序列化和json字符串用jackson解析成对象的比较
如果项目已经发布,如果临时想对某个在Memcached中的key修改值,那么以对象形式存储的话,将很难修改,但如果以字符串形式存储,通过json解析成对象的话,则会方便很多,因为通过界面往Memcached 添加字符串值是很简单的。
现在来比较一下这两种方式在时间消耗方面的差异:
package bean; import java.io.Serializable; public class User implements Serializable{ /** * 序列号 */ private static final long serialVersionUID = -3896605600471191953L; private int uid; private String uname; private String upass; private String abc; public String getAbc() { return abc; } public void setAbc(String abc) { this.abc = abc; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } }
(1)单个Object
把对象和json字符串放入memcached:
User user = new User(); user.setUid(123); user.setUname("Kingson_Wu"); user.setUpass("VIPS"); user.setAbc("acvb"); cache.set("Kingson_object", user); // Convert object to JSON string String Json = mapper.writeValueAsString(user); System.out.println("Change Object to JSON String: " + Json); //cache.set("Kingson_json", Json); cache.set("Kingson_json", " {\"uid\":123, \"uname\":\"Kingson_Wu\",\"upass\":\"VIPS\",\"abc\":\"acvb\"} ");
分别从memcached取出对象和json字符串进行转化:
ObjectMapper mapper = new ObjectMapper(); long start1 = System.nanoTime(); User userObject = (User) cache.get("Kingson_object"); // System.out.println(userObject.getUid() + userObject.getUname() + userObject.getUpass()); long end1 = System.nanoTime(); System.out.println("time1:" + (end1 - start1)); long start2 = System.nanoTime(); User userJson = (User) mapper.readValue((String) cache.get("Kingson_json"), User.class); // System.out.println(userJson.getUid() + userJson.getUname() + userJson.getUpass()); long end2 = System.nanoTime(); System.out.println("time2:" + (end2 - start2));
结果:
123Kingson_WuVIPS
time1:14086635
123Kingson_WuVIPS
time2:39176438
(2)List对象
把对象和json字符串放入memcached:
List<User> list=new ArrayList<>(); list.add(user); list.add(user); cache.set("list_Object", list); String JsonList = mapper.writeValueAsString(list); cache.set("list_json", JsonList); System.out.println(JsonList);
分别从memcached取出对象和json字符串进行转化:
long s1 = System.nanoTime(); List<User> list_Object= (List<User>)cache.get("list_Object"); //System.out.println(list_Object.size()); long e1 = System.nanoTime(); System.out.println("time1:" + (e1 - s1)); long s2 = System.nanoTime(); JavaType javaType = getCollectionType(ArrayList.class, User.class); List<User> list_json = (List<User>)mapper.readValue((String)cache.get("list_json"), javaType); //System.out.println(list_json.size()); long e2 = System.nanoTime(); System.out.println("time2:" + (e2 - s2));
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) { ObjectMapper mapper = new ObjectMapper(); return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); }
结果:
time1:1885873
time2:7717003
可以看出json字符串转化的时间是对象反序列化的三倍,但是json字符串转化的方式对于扩展来说比较灵活,至于使用哪种方式,看实际需求选择。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。