A, Memcached Overview
Memcached is a high-performance distributed memory object caching system for dynamic Web applications to reduce database load. It is to reduce the number of cache data read from the database by the object in memory and to improve the speed of the dynamic database-driven site. Memcached based on a store key / value pairs hashmap. Its daemon (daemon) is written in C language, but the client can be written in any language, and by agreement with the memcached daemon communication.
Second, the server installation Memcached
1, download the installation package
[root@instance-mtfsf05r ~]# wget https://memcached.org/files/memcached-1.5.16.tar.gz
2, unzip
[root@instance-mtfsf05r ~]# tar -zxvf memcached-1.5.16.tar.gz
3, enter the directory
[root@instance-mtfsf05r ~]# cd memcached-1.5.16
4. Compile
[root@instance-mtfsf05r memcached-1.5.16]# make && make test
5, the installation
[root@instance-mtfsf05r memcached-1.5.16]# make install
6, running Memcached server
[root@instance-mtfsf05r memcached-1.5.16]# memcached -d -m 10 -u root -l 10.211.55.4 -p 12000 -c 256 -P /tmp/memcached.pid
Third, the client connection Memcached server
1, Python module installed python-memcached
pip3 install python-memcached
2, python-memcached cluster operation module
import memcache
# 支持集群,将数据写在不同的机器
mc = memcache.Client(['106.12.115.136:'], debug=True)
# 可以设置权重
# mc = memcache.Client([('106.12.115.136:12000', 1), ('106.12.115.136:12000', 2)], debug=True)
mc.set('key', 'value')
ret = mc.get('key')
print(ret)
# 相关底层,将key转换成数字,将数字对服务器个数进行取余操作
# import binascii
# def cmemcache_hash(key):
# return (((binascii.crc32(key) & 0xffffffff) >> 16) & 0x7fff) or 1
# serverHashFunction = cmemcache_hash
3, add a method of operating Memcache
add method: used to create a key-value pair, if the key already exists, an exception will be reported
import memcache
mc = memcache.Client(['106.12.115.136:5004'], debug=True)
mc.set('key1', 'value1')
mc.set('key1', 'value2')
4, replace the method of operation Memcache
replace method: the key value is used to modify the value will be reported if the key does not exist exception.
import memcache
mc = memcache.Client(['106.12.115.136:5004'], debug=True)
mc.replace('key1', 'value2')# 报异常
5, set Memcache sum operation method set_multi
set: to create a key, if there is key to modify its value, if it does not exist to create the key-value pair
set_multi: create a number of key-value pairs, if the key (keys) exist on the amendment it (they) value, If the key does not exist (keys), then create it (them).
import memcache
mc = memcache.Client(['106.12.115.136:'], debug=True)
mc.set('key1', 'v1')
# 可以设置超时时间
mc.set('key1','v1',10) # 设置的超时时间是10秒
mc.set_multi({'key1': 'v1', 'key2': 'v2'})
# 可以设置超时时间
mc.set_multi({'key1': 'v1', 'key2': 'v2'},10)
6, delete Memcache operations sum delete_multi
delete: delete the specified key-value pairs
delete_multi: a plurality of keys to delete the specified
import memcache
mc = memcache.Client(['106.12.115.136:'], debug=True)
mc.set('key1', 'v1')
mc.set_multi({'key1': 'v1', 'key2': 'v2'})
mc.delete('key1')
mc.delete_multi(['key1', 'key2'])
7, get Memcache operations sum get_multi
get: Gets a key-value pair
get_multi: acquiring a plurality of key-value pairs
import memcache
mc = memcache.Client(['106.12.115.136:'], debug=True)
mc.set('key1', 'v1')
mc.set_multi({'key1': 'v1', 'key2': 'v2'})
mc.delete('key1')
mc.delete_multi(['key1', 'key2'])
mc.get('key1')
mc.get_multi(['key1', 'key2'])
8, append Memcache operate sum prepend
append: Modify the specified key value, the value after the additional content
prepend: modify specified key value, an insert in the front of the value
import memcache
mc = memcache.Client(['106.12.115.136:'], debug=True)
mc.set('key1', 'v1')
mc.append('key1', 'after') #key:v1after
mc.prepend('key2', 'before') #key:beforev1
9, dec Memcache operate sum incr
incr: a key value corresponding to an increase in Memcached n (n is 1 by default)
DECR: to reduce the n value corresponding to a key in Memcached (n is 1 by default)
import memcache
mc = memcache.Client(['106.12.115.136:'], debug=True)
mc.set('key1', '1')
# incr方法
mc.incr('key1')
mc.incr('key1', 1)
# decr方法
mc.decr('key1')
mc.decr('key1', 1)
10, gets Memcache operate sum cas
Such a program code paragraphs:
test01.py :
import memcache
mc = memcache.Client([('127.0.0.1:6000'), 1], debug=True)
mc.set('key1', 1000) #设置key1初始值1000
import memcache
mc = memcache.Client([('127.0.0.1:6000'), 1], debug=True)
ret = mc.get('key1')
print(ret) # 得到1000
i = input('回车结束!')
mc.set('key1', 999) # 设置key1为999
import memcache
mc = memcache.Client([('127.0.0.1:6000'), 1], debug=True)
ret = mc.get('key1')
print(ret)
i = input('回车结束!')
mc.set('key1', 999) # 设置key1为999
If test02.py and test03.py on behalf of the user to perform two procedures, if you do buy goods with both programs, it is known commodity stocks key1 1000. They also purchase, respectively, set the value of key1 to 999 after purchase. The question is, in fact key1 value should be 998, how to solve? To avoid this problem, Memcached provides a solution, the following is the improved program code:
test02.py :
import memcache
mc = memcache.Client([('127.0.0.1:6000'), 1], debug=True,cache_cas = Ture)
ret = mc.gets('key1')
print(ret) # 得到1000
i = input('回车结束!')
mc.cas('key1', 999) # 设置为999
import memcache
mc = memcache.Client([('127.0.0.1:6000'), 1], debug=True, cache_cas = Ture)
ret = mc.gets('key1')
print(ret) # 得到1000
i = input('回车结束!')
mc.cas('key1', 999) # 发生错误
Data can be acquired at the same time, but for setting data, if there is a program to modify the data (settings), then another modification (write back) will be wrong. With Memcached gets and cas method provided can solve this problem.