Python-based Memcached installation and operation, pythonmemcached

Source: Internet
Author: User

Python-based Memcached installation and operation, pythonmemcached

1. Memcached

Memcached is a high-performance distributed memory object Cache System for dynamic Web applications to reduce database load. It caches data and objects in the memory to reduce the number of reads to the database, thus improving the speed of dynamic and database-driven websites. Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can write it in any language and communicate with the daemon through memcached protocol.

Ii. basic use of memcached

1. Install memcached:

Wget http://memcached.org/latesttar-zxvf memcached-1.x.x.tar.gzcd memcached-1.x.x./configure & make test & sudo make install PS: Dependent libevent yum install libevent-devel apt-get install libevent-dev

2. Start Memcached

Memcached-d-m 10-u root-l 10.211.55.4-p 12000-c 256-P/tmp/memcached. pid parameter description:-d is to start a daemon-m is the amount of memory allocated to Memcache, the Unit is MB-u. the user running Memcache-l is the Server IP address of the listener-p is the port used to set the Memcache listener, it is best to set port-c above 1024 to the maximum number of concurrent connections. The default value is 1024. Set-P Based on the load of your server to set the pid file for saving Memcache.

3. Memcached command

Storage command: set/add/replace/append/prepend/cas command: get/gets other command: delete/stats ..

Use Python to operate Memcached

3. Install APIs

12 Use python to operate Memcached-Memcached ModuleDownload and install: https://pypi.python.org/pypi/python-memcached

1. First operation

123456 import memcache mc = memcache.Client(['10.211.55.4:12000'], debug=True)mc.set("foo""bar")ret = mc.get('foo')print ret

Ps: debug = True indicates the actual error message when an error occurs during running. this parameter is removed after going online.

2. clusters supported by nature

The python-memcached module supports cluster operations. The principle is to maintain a host list in the memory, and the weight of the host in the cluster is proportional to the number of times that the host appears repeatedly in the list.

1234567      Host weight    1.1.1.11    1.1.1.22    1.1.1.31 In the memory, the host list is:    host_list = ["1.1.1.1""1.1.1.2""1.1.1.2""1.1.1.3", ]

If you want to create a key-Value Pair (for example, k1 = "v1") in the memory, perform the following steps:

  • Convert k1 to a number based on the algorithm.
  • Calculate the remainder of the number and host list length to obtain a value of N (0 <= N <list length)
  • In the host list, obtain the host based on the value obtained in step 1 as the index, for example, host_list [N].
  • Connect to the host obtained in step 1 and place k1 = "v1" in the memory of the server.

The code is implemented as follows:

123 mc = memcache.Client([('1.1.1.1:12000'1), ('1.1.1.2:12000'2), ('1.1.1.3:12000'1)], debug=True) mc.set('k1''v1')

3. add
Add a key-value pair. If an existing key exists, the add operation is repeated.

1234567 #!/usr/bin/env python# -*- coding:utf-8 -*-import memcache mc = memcache.Client(['10.211.55.4:12000'], debug=True)mc.add('k1''v1')# Mc. add ('k1 ', 'v2') # An error is reported. duplicate existing keys are added. An error occurred !!!

4. replace
Replace modifies the value of a key. If the key does not exist, an exception occurs.

1234567 #!/usr/bin/env python# -*- coding:utf-8 -*-import memcache mc = memcache.Client(['10.211.55.4:12000'], debug=True)# If kkkk exists in memcache, the replacement is successful. Otherwisemc.replace('kkkk','999')

5. set and set_multi

Set sets a key-value pair. If the key does not exist, it is created. If the key exists, it is modified.
Set_multi sets multiple key-value pairs. If the key does not exist, it is created. If the key exists, it is modified.

123456789 #!/usr/bin/env python# -*- coding:utf-8 -*-import memcache mc = memcache.Client(['10.211.55.4:12000'], debug=True) mc.set('key0''wupeiqi') mc.set_multi({'key1''val1''key2''val2'})

6. delete and delete_multi

Delete deletes a specified key-Value Pair in Memcached.
Delete_multi Delete multiple key-value pairs in Memcached

12345678 #!/usr/bin/env python# -*- coding:utf-8 -*-import memcache mc = memcache.Client(['10.211.55.4:12000'], debug=True) mc.delete('key0')mc.delete_multi(['key1''key2'])

7. get and get_multi

Get gets a key-Value Pair
Get_multi gets one more key-Value Pair

12345678 #!/usr/bin/env python# -*- coding:utf-8 -*-import memcache mc = memcache.Client(['10.211.55.4:12000'], debug=True) val = mc.get('key0')item_dict = mc.get_multi(["key1""key2""key3"])

8. append and prepend

Append: Modify the value of the specified key, and append content to the value.
Prepend: Modify the value of the specified key and insert content before the value.

123456789101112 #!/usr/bin/env python# -*- coding:utf-8 -*-import memcache mc = memcache.Client(['10.211.55.4:12000'], debug=True)# k1 = "v1" mc.append('k1''after')# k1 = "v1after" mc.prepend('k1''before')# k1 = "beforev1after"

9. decr and incr 

Incr auto-increment: add N to a value in Memcached (N is 1 by default)
Decr auto-subtraction: reduce a value in Memcached by N (N is 1 by default)

123456789101112131415161718 #!/usr/bin/env python# -*- coding:utf-8 -*-import memcache mc = memcache.Client(['10.211.55.4:12000'], debug=True)mc.set('k1''777') mc.incr('k1')# k1 = 778 mc.incr('k1'10)# k1 = 788 mc.decr('k1')# k1 = 787 mc.decr('k1'10)# k1 = 777

10. gets and cas

For example, if the number of items remaining in the mall is changed to memcache, product_count = 900
User A refreshes the page and reads product_count = 900 from memcache.
User B refreshes the page and reads product_count = 900 from memcache.

If users A and B both purchase the product

User A modifies the remaining number of items. product_count = 899
User B: Change the remaining number of items. product_count = 899

As a result, the data in the cache is not correct. After two users buy the product, the remaining goods is still 899.
If you use python set and get to perform the preceding operations, the program will be shown in the preceding figure!

If you want to avoid this situation, you only need to use gets and cas, such:

123456789 #!/usr/bin/env python# -*- coding:utf-8 -*-import memcachemc = memcache.Client(['10.211.55.4:12000'], debug=True, cache_cas=True) = mc.gets('product_count')# ...# If someone modifies product_count after gets and Before cas, the following settings will fail to be executed and an exception will be thrown out to avoid the generation of abnormal data.mc.cas('product_count'"899")

Ps: essentially, each time gets is executed, an auto-increment number is obtained from memcache. When the value of gets is modified through cas, the obtained self-increment values are compared with the self-increment values in memcache. If they are equal, they can be submitted. If you do not want to wait, they are between gets and cas execution, another person executes gets (obtains the specified value of the buffer), so that abnormal data may occur, and modification is not allowed.

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.