Python's memcached installation and operation

Source: Internet
Author: User
Tags cas memcached

First, Memcached

Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.

Second, the installation of memcached basic use

1, memcached Installation:

wget http://memcached.org/-zxvf memcached-1. X.X.TAR.GZCD memcached-1. x.x. /configure && make && make test && sudo make install PS: dependent libevent       Yum install libevent-devel       apt-get install Libevent-dev

2. Start memcached

Memcached-d-M    -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, in megabytes    -u is the user    running Memcache -L is the server IP address    of the listener - P is the port to set memcache listening, preferably more than 1024 ports     -TheC option is the maximum number of concurrent connections running, the default is 1024, according to the load on    your server to set-P is the PID file to save Memcache

3. memcached command

Store command: set/add/replace/append/prepend/cas get command: Get/gets other commands: Delete/stats.

Python Operation memcached

Third, install the API

12 python操作Memcached使用Python-memcached模块下载安装:https://pypi.python.org/pypi/python-memcached

1. First operation

123456 importmemcachemc =memcache.Client([‘10.211.55.4:12000‘], debug=True)mc.set("foo""bar")ret =mc.get(‘foo‘)printret

Ps:debug = True indicates a real-world error message when a run error occurs, and the parameter is removed when it is online.

2, Natural support cluster

The python-memcached module natively supports cluster operations by maintaining a list of hosts in memory, and the weight values of the hosts in the cluster are proportional to the number of occurrences of the host in the list.

1234567      主机    权重    1.1.1.11    1.1.1.22    1.1.1.31那么在内存中主机列表为:    host_list =["1.1.1.1""1.1.1.2""1.1.1.2""1.1.1.3", ]

If the user is to create a key-value pair in memory (for example: K1 = "V1"), then perform the steps:

    • Convert K1 into a number based on the algorithm
    • Calculate number and host list length to remainder, get a value n (0 <= N < list length)
    • Gets the host in the host list according to the value obtained in 2nd step, for example: Host_list[n]
    • Connect the host acquired in step 3rd, place k1 = "V1" In the server's memory

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, repeat the add operation exception if the key already exists

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 ') # error, repeat add to existing key, FAIL!!!

4, replace
Replace to modify the value of a key, or exception if key does not exist

1234567 #!/usr/bin/env python #-*-coding:utf-8-*- import  memcache  mc  =  memcache. Client ([ ' 10.211.55.4:12000 ' ], debug = true # if Memcache is present in KKKK, the substitution succeeds, otherwise a field mc.replace ( ' kkkk ' ' 999 ' )

5. Set and Set_multi

Set sets a key-value pair, if key does not exist, is created if key exists, then modifies
Set_multi set multiple key-value pairs, if key does not exist, create if key exists, modify

123456789 #!/usr/bin/env python# -*- coding:utf-8 -*-importmemcachemc =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 a specified number of 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 get more than one key-value pair

12345678 #!/usr/bin/env python# -*- coding:utf-8 -*-importmemcachemc =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 modifies the value of the specified key, appending the content after the value
Prepend Modify the value of the specified key to insert the contents before the value

123456789101112 #!/usr/bin/env python# -*- coding:utf-8 -*-importmemcachemc =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 increment, add a value in memcached n (n default = 1)
DECR, reducing a value in memcached by n (n default = 1)

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

10, gets and CAs

such as the number of goods in the mall, assuming that the change is stored in memcache, Product_count = 900
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 not correct, two users after the purchase of goods, the product surplus or 899
If you use Python's set and get to manipulate the above procedure, the program will look like the above situation!

If you want to avoid this situation, just use get and CAS, such as:

123456789 #!/usr/bin/env python# -*- coding:utf-8 -*-importmemcachemc =memcache.Client([‘10.211.55.4:12000‘], debug=True, cache_cas=True)=mc.gets(‘product_count‘)# ...# 如果有人在gets之后和cas之前修改了product_count,那么,下面的设置将会执行失败,剖出异常,从而避免非正常数据的产生mc.cas(‘product_count‘"899")

Ps: In essence, each time a get is executed, it gets a self-increment number from the memcache, and when the value of the get is modified by CAS, it is compared with the self-increment value obtained before and memcache, if it is equal, it can be submitted, if not, 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 it is possible that abnormal data is not allowed to modify

Python's memcached installation and operation

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.