Python's memcache use

Source: Internet
Author: User
Tags cas memcached

Brief introduction

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 Web sites by caching data and objects in memory to reduce the number of times the 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 program in any language and communicate with the daemon through the memcached protocol.

memcached Installation
    • Server-side installation memcached:
Use wget to http://memcached.org download the latest source TAR-ZXVF memcached-x.x.x.tar.gzcd memcached-x.x.x./configure && Make & & make Test && sudo make installps: dependent on libevent, requires a pre-installation of Yum install Libevent-develapt-get install Libevent-dev
    • Start memcached
Memcached-d-M 10-u root-l 0.0.0.0-p 12000-c 256-p/tmp/memcached.pid parameter description:    -D is the boot    of a daemon-m is allocated to the memory used by memcache The number in MB-    U is the user running Memcache-    L is the server IP address of the listener-    p is the port that sets Memcache listening, preferably more than 1024 of the port-    C option is the maximum number of concurrent connections to run, The default is 1024, according to the load of your server to set-    p is set to save memcache PID file
    • memcached command
Store command: Set/add/replace/append/prepend/cas get command: Get/gets other command: Delete/stats.

Python Operation memcached

Install API

    1. Python operation memcached using python-memcached module
    2. Download install: https://pypi.python.org/pypi/python-memcached or pip install python-memcached

Simple examples of operations:

#!/usr/bin/env python3#coding:utf8import memcache# Link MC = memcache. Client ([' 139.129.5.191:12000 '], debug=true) #插入mc. Set ("name", "python") #读取ret = Mc.get (' name ') print (ret) # output Python # Debug=true indicates that an error message can be displayed when a run error occurs, and the formal environment can be

Natively supported clusters:

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.

Host IP        weight 1.1.1.1        11.1.1.2        21.1.1.3        3

Then the in-memory host list is: Host_list = ["1.1.1.1", "1.1.1.2", "1.1.1.2", "1.1.1.3", "1.1.1.3", "1.1.1.3",]

If the user wants to create a key-value pair in memory (for example: K1 = "value1"), perform the following steps:

    1. Convert K1 into a number based on the algorithm
    2. Remainder of number and host list length, get a value n (0 <= n < length)
    3. Gets the host for the index in the host list based on the value obtained in the second step, for example: Host_list[n]
    4. Connect the host acquired in the third step, place k1 = "value1" in the server's memory

The code is as follows:

#!/usr/bin/env python3#coding:utf8import MEMCACHEMC = memcache. Client ([' 1.1.1.1:12000 ', 1), (' 1.1.1.2:12000 ', 2), (' 1.1.1.3:12000 ', 3)]) Mc.set (' K1 ', ' value1 ') ret = mc.get (' K1 ') Print (ret)

Basic memcached operation

    • Add adds a key value pair, and if the key already exists, repeat the add operation with an exception
#!/usr/bin/env python#-*-coding:utf-8-*-import memcachemc = memcache. Client ([' 0.0.0.0:12000 ']) mc.add (' K1 ', ' v1 ') mc.add (' K1 ', ' v2 ') # error, repeat add to existing key, FAIL!!! For example: Ret1 = Mc.add (' name ', ' Tom ') print (refalse) Ret2 = Mc.add (' name ', ' Jack ') print (retrue) Result: False #当已经存在key Then return to Falsetrue  #如果不存在key     then return Treue   
    • replace Replace modifies the value of a key, and if key does not exist, the exception
#!/usr/bin/env python#-*-coding:utf-8-*-import memcachemc = memcache. Client ([' 0.0.0.0:12000 ']) mc.set (' name ', ' Tom ') re = mc.get (' name ') print (re) rereplace = mc.replace (' name ', ' Jack ') re = Mc.get (' name ') print (rereplace,re) results: Tom  #第一次赋值True Jack #如果存在key那么修改成功为yaoyao back to Truerereplace = Mc.replace (' Name1 ', ' hahaha ') ' re = Mc.get (' name1 ') print (rereplace,re) Result: False None #如果不存在key, modify failed, return null value
    • Set and Set_multi

Set: Sets a key-value pair that, if key does not exist, is created and modified if key exists.

Set_multi: Sets multiple key-value pairs, if key does not exist, is created and modified if key exists.

Import MEMCACHEMC = Memcache. Client ([' 0.0.0.0:12000 ']) mc.set (' name ', ' Tom ') ' re = Mc.get (' name ') print (' Set usage ', re) #设置一个键值对dic = {' name ': ' To, ', ' age ' : ' + ', ' job ': ' It '}mc.set_multi (DIC)  #设置多个键值对 # or Mc.set_multi ({' name ': ' Tom ', ' age ': ' + ', ' job ': ' It '}) Mcname = Mc.get (' name ') Mcage = Mc.get (' age ') McJob = mc.get (' job ') print (' Set_multi usage: ', mcname,mcage,mcjob)
    • Delete and Delete_multi

Delete: Deletes a specified key-value pair in memcached

Delete_multi: Delete specified multiple key-value pairs in memcached

Import MEMCACHEMC = Memcache. Client ([' 0.0.0.0:12000 ']) mc.set (' name ', ' Tom ') ' re = Mc.get (' name ') print (' presence ', re) mc.delete (' name ') Re = mc.get (' name ') Print (' delete ', re)  #删除一个键值对
    • Get and Get_multi

Get: Gets a key-value pair

Get_multi: Get multiple key-value pairs

#!/usr/bin/env python#-*-coding:utf-8-*-import memcachemc = memcache. Client ([' 0.0.0.0:12000 ']) mc.set (' name ', ' Tom ') ' re = Mc.get (' name ') print (' Get ', re)     #获取一个键值对dic = {' name ': ' To, ', ' Age ': ' + ', ' job ': ' IT '}mc.set_multi (DIC) regetmu=mc.get_multi ([' Name ', ' age ', ' job ']) print (' Get_multi ', re) # Get values for multiple key-value pairs
    • Append and prepend

Append: Modifies the value of the specified key to append content after the value .

Prepend: Modifies the value of the specified key to insert the contents before the value.

Import Memcache mc = Memcache. Client ([' 0.0.0.0:12000 ']) mc.set (' num ', ' first | ') Re = mc.get (' num ') print (re) mc.append (' num ', ' append second ') #在第一后面追加re = mc.get (' num ') print (re) mc.prepend (' num ', ' I am 0 ')  #在第一前面追加re = mc.get (' num ') print (RE) results: first | first | Append second I am 0 first | append second
    • DECR and INCR

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

INCR: Self-increment, reduce a value in memcached by n (n default = 1)

    • Gets and CAs

Using a cache system to share data resources is bound to bypass the problem of data contention and dirty data (data clutter).

Suppose that the remaining number of a product in a mall is saved in memcache, Product_count = 900

A user Refresh page reads from Memecache to Product_count = 900

b User Refresh page read from Memecache to Product_count = 900

A, a, a user buys a product and modifies the value of Product_count

A modified, Product_count = 899

b after modification, Product_count = 899

However the correct number should be 898, the data is chaotic.

If you want to avoid this situation, you can use Get and CAs

#!/usr/bin/env python#-*-coding:utf-8-*-import memcachemc = memcache. Client ([' 0.0.0.0:12000 '],cache_cas=true) mc.set (' count ', ' ten ') Reget = Mc.get (' count ') print (' number ', reget) regets = Mc.gets (' Count ') print (regets) # If someone modifies the product_count after the get and the CAs, the following settings will fail to be performed, splitting the exception to avoid abnormal data generation Recas = Mc.cas (' Count ', ' one ') print (recas) regets = mc.gets (' count ') print (' Modify ', regets)

Essentially, each time a 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 and memcache, and if it is equal, it can be committed, if not equal, This means that the change is not allowed between the get and the CAs execution, and the other person executes the gets.

  

  

Python's memcache use

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.