If the project has been published, if you temporarily want to change the value of a key in the memcached, then storing it as an object will be difficult to modify, but if stored as a string and parsed into an object by JSON, it will be much easier because the interface to memcached It is very simple to add a string value.
Now, compare the differences in time consumption between the two ways:
Package Bean;import Java.io.serializable;public class User implements serializable{ /** * Serial number * * 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) Single object
Put the object and the JSON string into 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\ "} ");
Remove the object and the JSON string from the memcached, respectively:
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));
Results:
123kingson_wuvips
time1:14086635
123kingson_wuvips
time2:39176438
(2) List Object
Put the object and the JSON string into 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);
Remove the object and the JSON string from the memcached, respectively:
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);
Results:
time1:1885873
time2:7717003
It can be seen that the JSON string conversion time is three times times the object deserialization, but the way JSON string conversion is more flexible for the extension, as for which way to see the actual needs of choice.
Comparison of object deserialization and JSON strings in memcached to objects parsed by Jackson