1.下载安装
1
2
3
4
5
|
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
1
|
.
/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.连接测试
1
2
3
4
5
6
7
|
[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
1
|
wget https:
//pypi
.python.org
/packages/f7/62/14b2448cfb04427366f24104c9da97cf8ea380d7258a3233f066a951a8d8/python-memcached-1
.58.
tar
.gz
|
linux环境:
1
2
3
|
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启动
1
2
3
|
.
/memcached
-d -u root -p 11211
.
/memcached
-d -u root -p 11212
.
/memcached
-d -u root -p 11213
|
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/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
本文转自 huangzp168 51CTO博客,原文链接:http://blog.51cto.com/huangzp/2045119,如需转载请自行联系原作者