Start memcachedmemcached-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 process-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-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 server-P is a PID file that is set to save memcache memcached Command Store command: Set/add/replace/append/prepend/CAs get command: Get/gets other commands: Delete/stats. Python operation memcached using Python-memcached Module1, 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 Operation memcached