(vii) Common methods of 7-4memcache

Source: Internet
Author: User

Memcache Common methods

Storage command: set/add/replace/append/prepend/
Get command: Get/gets
Other commands: Delete/stats

Add method: Adds a key value pair, if key exists, repeat add will error
Replace method: Replace modifies the value of a key, or the exception if key does not exist.

Import  MEMCACHEMC = Memcache. Client ([(' 192.168.88.102:11211 ')],debug=true) #add (key,value) mc.add ("Name5", "Cnblogs") Print Mc.get ("Name5") # Replacemc.replace ("Name5", "Hello World") print Mc.get ("Name5") Mc.set ("K2", "V2") Print Mc.get ("K2")

Operation Result:

Cnblogshello Worldv2

  

Set and Set_multi methods
Set: Sets a key-value pair, if key does not exist, is created if key exists, then modified;
Set_multi: Sets multiple key-value pairs, if key does not exist, is created and modified if key exists.

Import  MEMCACHEMC = Memcache. Client ([(' 192.168.88.102:11211 ')],debug=true) Mc.set_multi ({"k100": "V100", "k101": "v101", "k102": "v102"}) Print Mc.get_multi (["K100", "k101", "k102"])

Operation Result:

{' k100 ': ' v100 ', ' k101 ': ' v101 ', ' k102 ': ' v102 '}

  

Import  MEMCACHEMC = Memcache. Client ([(' 192.168.88.102:11211 ')],debug=true) Mc.set_multi ({"Test1": "Vale2", "test2": "value2", "test3": "Value3", " Test4 ":" Value4 ",}) print Mc.get_multi ([" Test1 "," Test2 "," Test3 "," test4 "]) mc.delete (" Test1 ") Mc.delete_multi ([" Test2 "," Test3 "]) print Mc.get_multi ([" Test1 "," Test2 "," Test3 "," test4 "]) Run Result: {' test1 ': ' Vale2 ', ' test3 ': ' Value3 ', ' Test2 ': ' value2 ', ' test4 ': ' Value4 '} {' test4 ': ' Value4 '}

  

Append and prepend method append: Modifies the value of the specified key, appends the value to it, and prepend: Modifies the value of the specified key to insert the contents before the value. MC = Memcache. Client ([(' 192.168.88.102:11211 ')],debug=true) Mc.set_multi ({"Test1": "Vale2", "test2": "value2", "test3": "Value3", " Test4 ":" Value4 ",}) Mc.append (" Test1 "," Ling ") print Mc.get (" Test1 ") mc.prepend (" Test2 "," Hello ") print mc.get (" Test2 ") Running Result: vale2linghellovalue2

  

(vii) Common methods of 7-4memcache

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.