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'})
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 '}
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 '}
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