Python Operation Memcahed

Source: Internet
Author: User
Tags cas

memcache Common Commands

Storage command: Set/add/replace/append/prepend/cas

Get command: Get/gets

Other commands: Delete/stats

1) Add method

Adding a key-value pair, repeating the add operation will report an exception if a key already exists.


2) Replace method

Replace modifies the value of a key, or the exception if key does not exist.


3) 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.


4) The difference between the set and the Add method

Set = add + Replace

The Add method is used to add a data to the Memcache server to be cached; if memcache the server already exists to store the Key , at this time Add method call failed.

The set method is used to set the cache content of a specified key, which is the collection of the Add method and the Replace method.

Cases:

#!/usr/bin/env pythonimport MEMCACHEMC = memcache. Client (["192.168.2.230:11211"],debug=true) mc.add ("Y", "a") Print (Mc.get ("Y")) mc.replace ("Y", "B") Print (Mc.get ("Y" ) Mc.set ("Y", "C") Print (Mc.get ("Y")) Mc.set_multi ({"I": 1, "J": 2, "K": 3}) print (Mc.get_multi (["I", "J", "K"]))


Operation Result:

A

B

C

{' I ': 1, ' K ': 3, ' J ': 2}


5) Delete and the Delete_multi Method

Delete: Deletes a specified key-value pair in the memcached;

Delete_multi: Deletes the specified number of key-value pairs in memcached.


6) Get and Get_multi methods

Get: Gets a key value pair;

Get_multi: Get multiple key-value pairs


Cases:

#!/usr/bin/env pythonimport MEMCACHEMC = memcache. Client (["192.168.2.230:11211"],debug=true) Mc.set_multi ({"I": 1, "J": 2, "K": 3, "G": 4}) Mc.delete ("I") mc.delete_multi (["J", "K"]) print mc.get_multi (["I", "J", "K", "G"])


Operation Result:

{' G ': 4}


7) Append and Prepend methods

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.


Cases:

#!/usr/bin/env pythonimport MEMCACHEMC = memcache. Client (["192.168.2.230:11211"],debug=true) Mc.set_multi ({"I": 1, "J": 2, "K": 3, "G": 4}) Mc.append ("I", 5) Mc.prepend ("J ", 5) Print mc.get_multi ([" I "," J "," K "," G "])


Operation Result:

{' I ': +, ' K ': 3, ' J ': K, ' G ': 4}



8) INCR and DECR methods

incr: Self-increment, add a value in memcached n (n default = 1)

DECR: Auto-subtract, reduce a value in memcached by n (n default = 1)

Cases:

#!/usr/bin/env pythonimport MEMCACHEMC = memcache. Client (["192.168.2.230:11211"],debug=true) Mc.set ("no", +) MC.INCR ("No") print (Mc.get ("no")) MC.INCR ("No", 100) Print (Mc.get ("no")) MC.DECR ("No") print (Mc.get ("no")) MC.DECR ("No") print (Mc.get ("no"))


Operation Result:

101

201

200

150


9) Gets and CAs method

To avoid multiple users from Memcache, the number is reduced by 1, not more.

if no is modified after the get and before the CAs, then the following settings will fail, throwing an exception, thus avoiding the output of abnormal data; In essence, each time the get is executed, a self-increment number is obtained from the memcache, and when the value of the get is modified through the CAS, it is compared with the self-increment in the previously obtained self-increment and memcache, and if it is equal, it can be committed, if not equal, That means that between get and CAS execution, there are other people who perform the gets (the specified value of the buffer is obtained), so that if abnormal data is possible, no modification is allowed.

Cases:

#!/usr/bin/env pythonimport MEMCACHEMC = memcache. Client (["192.168.2.230:11211"],debug=true) Mc.set ("no", +) v = mc.gets ("no") print (v) v1 = Mc.cas ("No", v) print (v1)


Operation Result:

100

True





Python Operation Memcahed

Related Article

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.