Xmemcachedclient Instance's Add method and set method difference2015-06-21 22:36 1561 People Read reviews (0) Favorite report Category: Memcached (12)
When we do memcached access data, we get the xmemcachedclient instance , the way to save the data to memcached is
1 Xmemcachedclient.set (String key,int time,object value);
2 Xmemcachedclient.add (String key,int time,object value);
First of all, both methods must be able to save data to the memcached, but what is the difference between the two?
The internet looked up a lot of information, did not see an explanation, so I decided to try to test the analysis:
Test code:
Load spring Bean.xml file, inside the relevant configuration, here no longer retell view text print ApplicationContext context = new Classpathxmlapplicationcontext ("Beans.xml" ); Xmemcachedclient xmc= (xmemcachedclient) Context.getbean ("Memcachedclient"); View text Print ApplicationContext context = new Classpathxmlapplicationcontext ("Beans.xml"); Xmemcachedclient xmc= (xmemcachedclient) Context.getbean ("Memcachedclient");
Perform the following code to view the text print System.out.println (xmc.add ("Hello", 0, "PPMs")); View text print System.out.println (xmc.add ("Hello", 0, "PPMs"));
Result is True
It's normal.
But this line of code executes two times, the second time there will be errors, the reason is very simple, the Xmemcachedclient add method does not allow the key value equal, that is, there is a key in memory, the value of Hello, again execute
Add method , it will fail. And after testing, the second execution of Xmc.set ("Hello", 0, "PPMs"), will not be wrong, because the set method on the cache server, there is no corresponding key, the new Key-value,
If so, replace the value corresponding to the key.
All in all: the difference between the set and the Add method is that the Add method does not allow the key value to be the same, and if the second add key is the same, the store fails, and the set method allows key to be the same, if the same, The value corresponding to the key is replaced.
Note: Each time you do the test, the first add or set, the second Test, you should first remove the value corresponding to the key in memcached, otherwise the value has been in the cache server, affecting the test results.
The removal method is simple: xmc.delete ("Hello");