Day35--memcache Common methods

Source: Internet
Author: User
Tags memcached

Add () Adds a key value pair, and if key already exists, repeating the add operation will report an exception

1 mc.add ('name2'Lisi')2  Print(mc.get ('name2'))3#  Lisi

Replace modifies the value of a key, and if key does not exist, the report is abnormal

1 # mc.add (' name ', ' Wangwu ')    # Add an existing key, an exception occurred 2#  memcached:while expecting ' STORED ', Got unexpected response ' not_stored '

Set () sets a key-value pair, if key does not exist, is created, exists, then modified

1 mc.set ('name2'zhaoliu')2 Print (Mc.get ('name2')) 3 # Zhaoliu
The difference between add () and set ():
    • Add () Adds a data to the memcache to be cached, and the call fails when key is present
    • Set () sets the cache contents of a specified key, there is a change in key, there is no creation, set () is the collection of Add () and replace ()
1 mport Memcache2 3MC = Memcache. Client (['11.11.11.11:12000'], debug=True)4 5Mc.add ('name2','Lisi')6 Print(Mc.get ('name2'))7 #Lisi8 9 #mc.add (' name ', ' Wangwu ') # Add an existing key, an exception occurredTen #Memcached:while expecting ' STORED ', got unexpected response ' not_stored ' OneMc.set ('name2','Zhaoliu') A Print(Mc.get ('name2')) - #Zhaoliu - #### the #C:\Python27\python.exe d:/python/memcache/memcache2.py - #Lisi - #Zhaoliu - # + #Process finished with exit code

Set_muilt () sets multiple key-value pairs, key exists, modifies, does not exist, creates a key-value pair to be passed in as a dictionary

1Mc.set_multi ({'Key1':'v100','Key2':'v200','Key3':'v300','Key4':'v400','Key5':'v500'})
    • Get () Gets the value of a key
    • Get_muilt () Gets the value of more than one key, and multiple keys are passed in list mode, returning a Dictionary object

1 Print(Mc.get ('Key5'))2 Print(Mc.get_multi (['Key1','Key2','Key3','Key4','Key5']))3 #v5004 #{' Key3 ': ' v300 ', ' key2 ': ' v200 ', ' key1 ': ' v100 ', ' key5 ': ' v500 ', ' Key4 ': ' v400 '}
    • Delete () Deletes a specified key-value pair
    • Delete_muild () Deletes a specified number of key values to multiple keys in list mode

1Mc.delete ("Key1")2 Print(Mc.get ('Key1'))3 #None4Mc.delete_multi (['Key2','Key3','Key4'])5 Print(Mc.get_multi (['Key1','Key2','Key3','Key4','Key5']))6 #{' key5 ': ' V500 '}
    • Append () modifies the value of the specified key, appending the content after the value
    • Prepend () modifies the value of the specified key to insert the contents before the value

1Mc.add ('Test','Hello')2 Print(Mc.get ('Test'))3 #Hello4Mc.append ('Test',' World')5 Print(Mc.get ('Test'))6 #HelloWorld7Mc.prepend ('Test','Hi,')8 Print(Mc.get ('Test'))9 #Hi,helloworld

INCR (key[, n]) increments the value of a key by n (n default = 1)

1Mc.add ('Num','1101')2MC.INCR ('Num')3 Print(Mc.get ('Num'))4MC.INCR ('Num', 100)5 Print(Mc.get ('Num')

DECR (key[, n]) decrement, the value of one key is reduced by N (n default is 1)

1Mc.set ('Num',' +')2MC.DECR ('Num')3 Print(Mc.get ('Num'))4 #9995MC.DECR ('Num', 100)6 Print(Mc.get ('Num'))7 #899

Day35--memcache Common methods

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.