1. 程式人生 > >python連接memcached

python連接memcached

python

1.下載安裝

wget http://www.memcached.org/files/memcached-1.5.3.tar.gz
tar -zxvf memcached-1.5.3.tar.gz
cd memcached-1.5.3
./configure --prefix=/data/program/memcached
make && make install



2.啟動memcache

./memcached -d -u root -l 127.0.0.1 -m 50 -p 11211 -c 512 -P /data/program/memcached/logs/memcached.pid

參數:

-u 運行身份

-p 指定端口號(默認11211)

-m 指定最大使用內存大小(默認64MB)

-t 線程數(默認4)

-l 連接的IP地址, 默認是本機

-d 以後臺守護進程的方式啟動

-c 最大同時連接數,默認是1024

-P 制定memecache的pid文件

-h 打印幫助信息


3.連接測試

[root@huangzp3 bin]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
add test 1 0 5
hello
STORED



4.memcache集群的操作

4.1下載api模塊python-memcached

wget https://pypi.python.org/packages/f7/62/14b2448cfb04427366f24104c9da97cf8ea380d7258a3233f066a951a8d8/python-memcached-1.58.tar.gz

linux環境:

tar python-memcached-1.58.tar.gz
cd python-memcached-1.58
python setup.py install


參考鏈接:https://www.cnblogs.com/xiao1/p/5858967.html


4.2啟動

./memcached -d -u root -p 11211
./memcached -d -u root -p 11212
./memcached -d -u root -p 11213

例子:

#!/usr/bin/env python
import memcache

# 初始化一個memcache客戶端對象,端口權重分別為1:2:1,開啟錯誤debug顯示
mc = memcache.Client([("192.168.2.230:11211",1),("192.168.2.230:11212",2),("192.168.2.230:11213",1)],debug=True)
mc.set("name","huangzhenping")
print(mc.get("name"))

mc1 = memcache.Client(["192.168.2.230:11211"],debug=True)
print(mc1.get("name"))

mc2 = memcache.Client(["192.168.2.230:11212"],debug=True)
print(mc1.get("name"))

mc3 = memcache.Client(["192.168.2.230:11213"],debug=True)
print(mc3.get("name"))


運行結果:

huangzhenping

None

None

huangzhenping



python連接memcached