Linux and Python

Python 操作Redis

redis对比monoDB:

  redis和memcache 是key value非关系型数据库,mysql是关系型数据库,表的结构和保存的内容有严格的要求,关系型数据库无法保存临时数据或不标准的数据,redis支持的数据类型相比memcache要多,memcache只支持hash(哈希类型)。

  redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性(一组操作要么都完成要不不操作)的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是先缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步、cluster(集群)。

 

下载安装:

[root@localhost ~]# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
[root@localhost ~]# tar xvf redis-3.0.7.tar.gz
[root@localhost ~]# mv redis-3.0.7 /usr/local/redis
[root@localhost ~]# make
[root@localhost redis]# vim redis.conf  #更改配置:

[root@localhost redis]# grep -v "#" redis.conf  | grep -v "^$"  #简单配置如下:

daemonize yes
pidfile /var/run/redis.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename 10.16.59.103.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
redis.conf

 

启动脚本:

[root@localhost redis]# cp redis.conf  /etc/redis.conf #复制配置文件

[root@localhost redis]# cp utils/redis_init_script  /etc/init.d/redis 

[root@localhost redis]# vim /etc/init.d/redis 

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.


# chkconfig: - 30 69
# description:  Redis start script

REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
redis script

启动服务:

[root@localhost redis]# /etc/init.d/redis  start

确认服务器启动成功:

[root@localhost redis]# ps -ef | grep redis
root     40500     1  0 16:07 ?        00:00:00 /usr/local/redis/src/redis-server *:6379         
root     40663 37362  0 16:21 pts/1    00:00:00 grep redis
[root@localhost redis]# ss -tnlp | grep redis
LISTEN     0      128                       *:6379                     *:*      users:(("redis-server",40500,5))
LISTEN     0      128                      :::6379                    :::*      users:(("redis-server",40500,4))

安装python 客户端:

pip install redis

API使用

redis-py 的API的使用可以分类为:

  • 连接方式
  • 连接池
  • 操作
    • String 操作
    • Hash 操作
    • List 操作
    • Set 操作
    • Sort Set 操作
  • 管道
  • 发布订阅

1、操作模式

redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令,Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py。

#/usr/bin/env  python
# -*- coding:utf-8 -*-
import redis

r = redis.Redis(host='10.16.59.103', port=6379)
r.set('foo', 'Bar')
print(r.get('foo'))

执行结果:

 b'Bar'

 

posted @ 2016-03-25 14:27  jack.hang  阅读(825)  评论(0编辑  收藏  举报