Common methods for Python Memcached

Source: Internet
Author: User

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

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.