python访问redis

来源:互联网 发布:金蝶软件常见问题 编辑:程序博客网 时间:2024/06/01 09:58

python访问redis

1 Linux上安装redis

a) 下载

$ wget http://download.redis.io/releases/redis-3.0.5.tar.gz

b) 编译

# yum install gcc tcl# tar -zxf redis-3.0.5.tar.gz# cd redis-3.0.5# make# make test# sudo make install

这样可执行文件redis-server等就从redis-3.0.5/src拷贝到/usr/local/bin

c) 启动服务(6379端口要打开)

$ redis-server

$ redis-server /path/to/redis.conf

1428:M 04 Feb 11:47:33.817 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1428:M 04 Feb 11:47:33.817 # Server started, Redis version 3.0.5
1428:M 04 Feb 11:47:33.818 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1428:M 04 Feb 11:47:33.818 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

d) 服务器配置,以除去上面的警告

以root用户操作

1) 修改内核参数: somaxconn

该内核参数默认值一般是128,对于负载很大的服务程序来说大大的不够。一般会将它修改为2048或者更大。

# echo 2048 > /proc/sys/net/core/somaxconn
重启后保存:
在/etc/sysctl.conf中添加如下
net.core.somaxconn = 2048
然后在终端中执行
# sysctl -p

2) 修改vm.overcommit_memory

overcommit_memory 指定了内核针对内存分配的策略,其值可以是0、1、2。                 
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存

# vi  /etc/sysctl.conf

增加下面一行:

vm.overcommit_memory=1
设置重启后保持, 执行命令:

# sysctl vm.overcommit_memory=1

3) transparent_hugepage

禁用透明巨页内存配置以提高性能

查看当前状态:

$ cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never

参数说明
never 关闭,不使用透明内存
alway 尽量使用透明内存,扫描内存,有512个 4k页面可以整合,就整合成一个2M的页面
madvise 避免改变内存占用

禁用透明巨页内存:

# echo never >/sys/kernel/mm/transparent_hugepage/enabled# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]

永久禁用, 将命令加入到rc.local中:

# vi rc.local

...# redis required never for transparent_hugepageif test -f /sys/kernel/mm/transparent_hugepage/enabled; then   echo never > /sys/kernel/mm/transparent_hugepage/enabledfiif test -f /sys/kernel/mm/transparent_hugepage/defrag; then   echo never > /sys/kernel/mm/transparent_hugepage/defragfiexit 0 

e) redis.conf配置

参考下面的redis.conf的配置项: redis配置详解

http://blog.csdn.net/ithomer/article/details/9232891

2 redis-py

redis-py 这是 Redis 目前最成熟的 Python 客户端开发包。

redis 主页:

https://github.com/andymccurdy/redis-py

a) 下载源码

$ git clone https://github.com/andymccurdy/redis-py.git

b) 安装

$ cd redis-py$ sudo python redis install

3 hiredis

hiredis是redis数据库的C接口,目前只能在linux下使用,几个基本的函数就可以操作redis数据库了。hiredis 是 Redis 官方指定的 C 语言客户端开发包,支持 Redis 完整的命令集、管线以及事件驱动编程。

hiredis主页:

https://github.com/redis/hiredis

a) 下载安装

$ git clone https://github.com/redis/hiredis.git$ cd hiredis$ make$ sudo make install

例子:

http://blog.csdn.net/mfc_vc_andy/article/details/8095839

http://www.leoox.com/?p=304

4 hiredis-py

python对hiredis的包装类。

主页:

https://github.com/redis/hiredis-py

a) 下载

$ wget https://pypi.python.org/packages/source/h/hiredis/hiredis-0.2.0.tar.gz

b) 安装

$ tar zxf hiredis-0.2.0.tar.gz$ cd hiredis-0.2.0$ python setup.py build$ sudo python setup.py install

5 python操作redis

http://blog.csdn.net/chosen0ne/article/details/7319807

http://blog.csdn.net/chenggong2dm/article/details/6102540


6 优雅关闭redis-server

$ redis-cli -h 127.0.0.1 -p 6379 shutdown

7 redis-server启用密码及简单例子

a) 复制redis.conf到/etc/

b) 创建redis数据库存储目录

$ sudo mkdir /var/redis

c) 修改/etc/redis.conf

$ sudo vi redis.conf
仅仅修改的内容如下:

daemonize yesloglevel warninglogfile /var/log/redis.logdir /var/redis/requirepass Abc123

d) 启动和关闭服务

$ sudo redis-server /etc/redis.conf带密码关闭服务:$ redis-cli -h 127.0.0.1 -p 6379 -a Abc123 shutdown

e) 客户端python

此时客户端需要使用带密码连接服务器:

import redisr=redis.StrictRedis(host="127.0.0.1", port=6379, db=0, password="Abc123")r.set("city", "shanghai")r.get("city")

给key设置过期时间。比如我们仅仅保存60秒的数据,可以这样设置:

r.expire("city", 60)
紧接着访问它:

r.get("city")shanghai
过60秒再访问:

r.get("city")

没有了。

8 附录

更详细的例子参考:
https://github.com/andymccurdy/redis-py

redis的完整的参考:

http://www.redis.net.cn/tutorial/3505.html


0 0
原创粉丝点击