Common methods:
(1) Add Method:
Usage: Add (key, value)
Method Description: Added a new key-value pair to add a cached data to the Memcache server
Import Memcache
MC = Memcache. Client (' [IP segment] ', debug=true)???? #连接memcache
Mc.add (' K1 ', ' v1 ')
Print Mc.get (' K1 ')
Results:
V1
(2) Replace
Usage: replace (key, value)
Method Description: Replace the value of key (or reconfigure the value of key)
Mc.replace (' K1 ', ' hello ')
Print Mc.get (' K1 ')
Results:
Hello
(3) Set
Usage: Set (key, value)
Method Description: The Set method is used to set the cache content of a specified key, and the Set method is the aggregate of the Add method and the Replace method.
Mc.set (' K2 ', ' v2 ')
Print Mc.get (' K2 ')
Results:
V2
Attention:
① if the key to be set does not exist, the set method has the same effect as the Add method;
② if the key you want to set already exists, the Set method works the same as the Replace method.
Summarize the differences between set and add:
Set = add + Replace
If this key value exists, add will error, set will not error, will be re-assigned, and overwritten.
(4) Set_multi, Get_multi
Description: Set_multi (dict)? Set multiple key:value at once
Get_multi (list) gets multiple keys at a time, and each key is passed as a parameter in the form of a list, and the return type is a dictionary.
Mc.set_multi ({"k100": "V100", "k101": "v101", "k102": "v102"})
Print? (Mc.get_multi (["K100", "k101", "k102"])
Results:
{"K100": "V100", "k101": "v101", "k102": "v102"}
(5) Delete
Description
Mc.set_multi ({"Test1": "V1", "test2": "V2", "Test3", "V3"})
Mc.delete ("Test1")
Mc.delete (["Test2", "test3"])
Results:
return empty Dictionary {}
(6) Append and prepend
Mc.append ("Test1", "Ling")
Print (Mc.get ("Test1"))
Return:
V1Ling
Description: Appends data to the original value and merges it into one data
Mc.prepend ("Test2", "Hello")
Print (Mc.get ("Test2"))
Return:
Hellov2
Description: Adds data before the original value, merging into one data
(7) INCR and DECR (self-increment and self-reduction)
Mc.set ("shop", +)???? # Set Key,value value
MC.INCR ("Shop")????????????? # Default self-increment 1
Print (Mc.get ("Shop"))
MC.INCR ("shop", +)???????? # Manually increment by 100, the second parameter specifies the amount of increment.
Print (Mc.get ("Shop"))
Results:
1001
1101
MC.DECR ("Shop")???????????? # Default Auto minus 1
Print (Mc.get ("Shop"))
MC.DECR ("Shop", "$")???? # manually subtract 500, the second parameter specifies the amount of reduction.
Print (Mc.get ("Shop"))
Results:
1100
600
Common methods for Python Memcached