Python learning-seven weeks four lessons (December 7)

Source: Internet
Author: User
Tags cas

Seven weeks four sessions (December 7)
Common methods of 13.12/13.13 memcache

Storage command: Set/add/replace/append/prepend/cas
Get command: Get/gets
Other commands: Delete/stats.

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

MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
# Mc.set ("foo", "Bar")
# ret = mc.get ("foo")
Mc.add (' K1 ', ' v1 ')
Mc.add (' K1 ', ' v1 ')
# Print (ret)

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

MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
# Mc.set ("foo", "Bar")
# ret = mc.get ("foo")
Mc.add (' K1 ', ' v1 ')
# mc.add (' K1 ', ' v1 ')
# Print (ret)
Mc.replace ("K1", "666")
Print (Mc.get ("K1"))
Results:
666

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.
Import Memcache

MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
Mc.set ("K10", "V10")
Mc.set_multi ({"K11": "V11", "K12": "V12"})

The difference between the Set method and the Add method
Set = add + Replace
1. Memcache::add method: The Add method is used to add a data to the Memcache server to be cached.

Note: If the key to be stored already exists in the Memcache server, the Add method call fails at this time.

2. Memcache::set method: 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.

Attention:

1), if the key to be set does not exist, then the set method is consistent with the effect of the Add method;

2), if the key to be set already exists, the set method is the same as the Replace method.

3. Mmecache::replace method: The Replace method is used to replace the cached content of a specified key and returns False if key does not exist


Delete and Delete_multi methods
Delete: Deletes a specified key-value pair in the memcached;
Delete_multi: Deletes the specified number of key-value pairs in memcached.
Import Memcache

MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
# Mc.set ("K10", "V10")
# Mc.set_multi ({"K11": "V11", "K12": "V12"})
#
Mc.delete ("K10")
Mc.delete_multi (["K11", "K12"])

Get and Get_multi methods
Get: Gets a key value pair;
Get_multi: Gets multiple key-value pairs.
#!/usr/bin/env python
#-*-Coding:utf-8-*-
# @Time: 2017/9/20 10:37
# @Author: Lingxiangxiang
# @File: demon1.py

Import Memcache

MC = Memcache. Client ([' 192.168.48.128:11211 '], debug=true)
Mc.set ("K10", "V10")
Mc.set_multi ({"K11": "V11", "K12": "V12"})
#
# Mc.delete ("K10")
# Mc.delete_multi (["K11", "K12"])

val = mc.get (' K1 ')
Print (val)

Item_dict = Mc.get_multi ([' K11 ', ' K12 '])
Print (item_dict)

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.
#!/usr/bin/env python
#-*-Coding:utf-8-*-

Import Memcache

MC = Memcache. Client ([' 192.168.10.128:12000 '], debug=true)

Mc.append (' K1 ', ' after ')
Val1 = Mc.get (' K1 ')
Print (VAL1)

Mc.prepend (' K1 ', ' brefore ')
Val2 = Mc.get (' K1 ')
Print (VAL2)

#结果:
V1afterafter
Breforev1afterafter


INCR: Self-increment, add a value in the memcached to N (n default is 1);
DECR: Decrement, reduce a value in memcached by N (n default is 1).
#!/usr/bin/env python
#-*-Coding:utf-8-*-

Import Memcache

MC = Memcache. Client ([' 192.168.10.128:12000 '], debug=true)

Mc.set (' K1 ', ' 777 ')

#mc. INCR (' K1 ') #778, default self-increment 1

MC.INCR (' K1 ', 10)
Val1 = Mc.get (' K1 ')
Print (VAL1)

MC.DECR (' K1 ', 20)
Val2 = Mc.get (' K1 ')
Print (VAL2)

#结果

787 #自增后的结果
767 #自减后的结果

Gets and CAS:
such as the number of shopping mall goods, assuming that the value of the change in Memcache, Product_count =9000
A user Refresh page reads from memcache to Product_count = 900
b User Refresh page read from memcache to Product_count = 900
If both A and B users purchase goods:
A user modified the number of items remaining Product_count = 899
b User modified the number of items remaining Product_count = 899
As a result, the data in the cache is no longer correct, two users after the purchase of goods, the product remains 899, if the use of Python set and get to operate the above process, then the program will be as described above, the data is not accurate.
If you want to avoid this situation, just use get and CAS, such as:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Haifeng Di

Import Memcache

MC = Memcache. Client ([' 192.168.10.128:12000 '], debug=true)

v = mc.gets (' Product_count ')
Print (v)

#如果有人在gets之后和cas之前修改了product_count, the following setting will fail to throw an exception to avoid output of abnormal data
V1 = Mc.cas (' Product_count ', "899")
Print (v1)

#结果:
899
True
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.

Python learning-seven weeks four lessons (December 7)

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.