Xmemcachedclient instance's Add method and set method difference

Source: Internet
Author: User
Tags memcached
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");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.