python--12th Day Summary (Python operations RabbitMQ, Redis, Memcache, SQLAlchemy)

Source: Internet
Author: User
Tags cas memcached rabbitmq

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.

memcached installation and Basic use

Memcached Installation:

  • wget http://memcached.org/latest
  • tar -zxvf memcached-1.x.x.tar.gz
  • cd memcached-1.x.x
  • ./configure && make && make test && sudo make install
    • PS:依赖libevent
    •        yum install libevent-devel
    •        apt-get install libevent-dev

Start memcached

1Memcached-d-MTen-U root-l10.211.55.4-P12000-C the-p/tmp/Memcached.pid2  3 parameter Description:4-D is to start a daemon process5-m is the amount of memory allocated to Memcache, in megabytes6-U is the user running Memcache7-L is the server IP address of the listener8-P is the port to set memcache listening, preferably more than 1024 ports9-The c option is the maximum number of concurrent connections to run, which is 1024 by default and is set according to the amount of load on your serverTen-P is a PID file that is set to save Memcache

memcached command

    1. 存储命令: set/add/replace/append/prepend/cas
    2. 获取命令: get/gets
    3. 其他命令: delete/stats..

Python Operation memcached

Install API

python操作Memcached使用Python - memcached模块 下载安装:https: / / pypi.python.org / pypi / python - memcached
1, first-time operationImportMemcache MC= Memcache. Client (['10.211.55.4:12000'], debug=True) Mc.set ("Foo","Bar") ret= Mc.get ('Foo')PrintRetps:debug=True indicates that a real-world error message is running when an error occurs, and the parameter is removed when it is online. 2, natural support for cluster Python-The memcached module natively supports cluster operations by maintaining a host list in memory, and the weight value of the host in the cluster and the number of times the host repeats in the list is proportional to the host weight1.1.1.1 1 1.1.1.2 2 1.1.1.3 1then the in-memory host list is: 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, such as: K1="v1"), then perform the steps: Convert K1 to a number based on the algorithm to find the remainder of the number and host list length, and get a value of N (0<= N <list length) in the host list, the value obtained from the 2nd step is the index to get the host, for example: Host_list[n] connection will be taken in 3rd step of the host, will K1="v1"The in -memory code placed on the server is implemented as follows: 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 adds a key value pair, repeat the add operation exception if the key already exists#!/usr/bin/env python#-*-coding:utf-8-*-ImportMemcache MC= Memcache. Client (['10.211.55.4:12000'], debug=True) Mc.add ('K1','v1')#mc.add (' K1 ', ' v2 ') # error, added to the existing key repeatedly, failed!!! 4, Replacereplace modifies the value of a key, and if key does not exist, the exception#!/usr/bin/env python#-*-coding:utf-8-*-ImportMemcache MC= Memcache. Client (['10.211.55.4:12000'], debug=True)#If there is kkkk in the memcache, the substitution succeeds, otherwise aMc.replace ('KKKK','999')5, set, and Set_multiset set a key-value pair, if key does not exist, create if key is present, modify Set_multi to set multiple key-value pairs, if key does not exist , then create if key is present, modify#!/usr/bin/env python#-*-coding:utf-8-*-ImportMemcache 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_multidelete delete the specified key-value pair in memcached delete_multi Delete the specified number of key-value pairs in memcached#!/usr/bin/env python#-*-coding:utf-8-*-ImportMemcache MC= Memcache. Client (['10.211.55.4:12000'], debug=True) Mc.delete ('Key0') Mc.delete_multi (['Key1','Key2'])7, get, and Get_multiget get a key value pair get_multi get one more key value pair#!/usr/bin/env python#-*-coding:utf-8-*-ImportMemcache 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 prependappend Modify the value of the specified key, append the content after the value prepend modify the value of the specified key, insert the content before the value#!/usr/bin/env python#-*-coding:utf-8-*-ImportMemcache 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 increment, increase one of the values in memcached by n (n default = 1) decr decrement, decrease one of the values in memcached by n (default = 1)#!/usr/bin/env python#-*-coding:utf-8-*-ImportMemcache MC= 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 = 77710, gets and CAs, such as the number of goods in the mall, assuming that the value is stored in memcache, Product_count= 900a user Refresh page reads from memcache to Product_count= 900b User Refresh page read from memcache to Product_count= 900If a, B users are buying a product a user modified the number of items remaining product_count=899b User modified the number of items remaining product_count=899As a result, the data in the cache is not correct, and after two users have purchased the product, the remaining899If 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:#!/usr/bin/env python#-*-coding:utf-8-*-ImportMEMCACHEMC= Memcache. Client (['10.211.55.4:12000'], Debug=true, cache_cas=True) v= Mc.gets ('Product_count')# ...#If someone modifies the product_count after the get and the CAs, the following settings will fail and the exception is broken to avoid abnormal data generationMc.cas ('Product_count',"899"Ps: In essence, each time a get is executed, a self-increment number is obtained 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 that if abnormal data is possible, no modification is allowed. 

python--12th Day Summary (Python operations RabbitMQ, Redis, Memcache, SQLAlchemy)

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.