Redis入门指南之三(入门)

本节主要介绍Redis的5种数据类型,同时使用Python API来操作Redis,其中python版本为3.5, redis版本为4.0.2。

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

(1)连接方式

(2)连接池

(3)操作

  • String 操作
  • Hash 操作
  • List 操作
  • Set 操作
  • Sort Set 操作

(4)管道

(5)发布订阅

1. redis文件配置

(1)配置redis配置文件

    在之前的博客https://www.cnblogs.com/xuejiale/p/10424885.html中安装并配置了redis,为了能够使用python操作redis,还需要再次配置redis的配置文件/etc/redis/6379.conf。

  • 找到bind 127.0.0.1这一行,修改为bind 0.0.0.0

Redis入门指南之三(入门)

  • 把protected-mode yes改为no

Redis入门指南之三(入门)

  • 找到daemonize,可以看到reids默认情况下不是后台驻留程序,(将daemonize属性改为yes,表明需要在后台运行,这个根据你个人情况来搞,如果选择后台驻留,建议设一个密码)

Redis入门指南之三(入门)

(2)启动redis服务端

    使用指定配置文件的方式启动

[root@centos redis]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@centos redis]# /etc/init.d/redis_6379 start
Starting Redis server...
:C Mar ::23.397 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
:C Mar ::23.398 # Redis version=4.0., bits=, commit=, modified=, pid=, just started
:C Mar ::23.398 # Configuration loaded
[root@centos redis]# redis-cli
测试是否可以连接redis:
127.0.0.1:> PING
PONG
127.0.0.1:>

(3)其他操作

     在Python中运行时有时候会报相关错误,要把防火墙关掉

暂时关闭防火墙:(立即生效,开机重启,会重新打开)
service iptables stop
永久关闭防火墙(关机重启才会生效)
chkconfig iptables off

(3)redis设置密码

  • 在配置文件中设置

在配置文件中找到requirepass foobared行,打开注释并设置密码为redis123,requirepass redis123,保存退出并重启redis:

Redis入门指南之三(入门)

重启之后连接redis,并测试需要输入密码:

[root@centos redis]# redis-cli
127.0.0.1:> SET name lisi
(error) NOAUTH Authentication required.
127.0.0.1:> AUTH redis123
OK
127.0.0.1:> SET name lisi
OK
  • 在客户端设置

开两个窗口,在一个窗口设置,如下

127.0.0.1:> config set requirepass newredis123
OK

在另一个窗口登录:

[root@centos redis]# redis-cli
127.0.0.1:> SET name wangwu
(error) NOAUTH Authentication required.
127.0.0.1:> AUTH newredis123
OK
127.0.0.1:> SET name wangwu
OK
127.0.0.1:>

(4)python redis API安装与运行redis

首先要安装python 操作redis的API接口模块:

sudo pip install redis
or
sudo easy_install redis
or
源码安装 详见:https://github.com/WoLpH/redis-py

为了方便测试,目前及后续我们不设置redis的密码,使用Windows连接Linux上安装的redis,测试程序如下:

 import redis

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

python操作redis

执行结果:

Redis入门指南之三(入门)

2. 连接方式

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

默认情况下,往redis里插入数据后再读出来时所有键与值都是byte类型的,就是说如果你要使用这些值全部都得转换编码。为了读出的数据是string类型,有一个参数decode_responses可以设置,改参数默认为False,连接redis,加上decode_responses=True,写入的键值对中的value为str类型,不加这个参数写入的则为字节类型。例如:

 import redis

 r = redis.Redis(host='192.168.30.134', port=)
#r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True)
r.set('subject', 'python') print(r['subject']) # 如果subject不存在则会报错
print(r.get('subject')) # 如果subject不存在则会返回None
print(type(r.get('subject')))

无参数decode_responses参数

执行结果:

Redis入门指南之三(入门)

 import redis

 r = redis.Redis(host='192.168.30.134', port=, decode_responses=True)
#r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True)
r.set('subject', 'Go') print(r['subject']) # 如果subject不存在则会报错
print(r.get('subject')) # 如果subject不存在则会返回None
print(type(r.get('subject')))

有参数decode_responses参数

执行结果:

Redis入门指南之三(入门)

注意:如果你设置decode_responses=True,在往数据库里真的写二进制数据的时候,那么再读取的时候就会出错。除非你明确可以知道你没有使用二进制value。

3. 连接池

redis-py使用connection pool来管理对一个redis server的所有连接,避免每次建立、释放连接的开销。默认,每个Redis实例都会维护一个自己的连接池。可以直接建立一个连接池,然后作为参数Redis,这样就可以实现多个Redis实例共享一个连接池。

 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=)
#r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.set('foo', 'Bar')
print(r.get('foo'))

redis pool

4. 热身

本小节是通过命令行操作redis,后面不会再使用该命令行的方式,会使用python API操作redis。

首先了解几个基础命令,打开redis-cli

(1)获得符合规则的键名列表

KEYS pattern

可以通过help COMMAND来获取该命令的使用信息。比如:

127.0.0.1:> help KEYS

  KEYS pattern
summary: Find all keys matching the given pattern
since: 1.0.
group: generic

pattern支持glob风格通配符格式,具体规则如下:

Redis入门指南之三(入门)

使用KEYS *能够获的Redis中所有的键,如

127.0.0.1:> KEYS *
) "num"
) "key"
) "foo"
) "bar"
) "name"

注意:KEYS命令需要遍历Redis中所有的键,当键的数量较多时会影响性能,不建议在生产环境中使用。

提示:Redis不区分命令大小写,但习惯于使用大写字母表示Redis命令。

(2)判断一个键是否存在

EXISTS key [key ...]
summary: Determine if a key exists

如果键存在则返回整数类型1,否则返回0。

127.0.0.1:> EXISTS foo
(integer)
127.0.0.1:> EXISTS noexists
(integer)

(3)删除键

  DEL key [key ...]
summary: Delete a key

可以删除一个或者多个键,返回值是删除的键的个数。如:

127.0.0.1:> DEL foo
(integer)
127.0.0.1:> DEL foo
(integer)

第二次执行 DEL 命令时因为 bar 键已经删除了,实际上并没有删除任何键,所以返回0。

技巧: DEL命令的参数不支持通配符,但是可以结合Linux的管道和xargs命令自行实现删除所有符合规则的键。比如要删除所有以“user:”开头的键,就可以执行redis-cli KEYS "user:*" | xargs redis-cli DEL。另外由于DEL命令支持多个键作为参数,所以还可以执行redis-cli DEL `redis-cli KEYS "user:"`来达到同样的效果,但是性能更好。

(4)获得键值的数据类型

TYPE key
summary: Determine the type stored at key

TYPE命令用来获得键值的数据类型,返回值可能是string(字符串类型)、hash(散列类型)、list(列表类型)、set(集合类型)、zset(有序集合类型)。例如:

127.0.0.1:> SET foo
OK
127.0.0.1:> TYPE foo
string
127.0.0.1:> LPUSH bar
(integer)
127.0.0.1:> TYPE bar
list
127.0.0.1:>

5. 操作

(1)String 操作

String操作,redis中的String在在内存中按照一个name对应一个value来存储。字符串类型是Redis中基本的数据类型,他能存储任何形式的字符串,包括二进制数据。你可以用其存储用户的邮箱、JSON化的对象甚至是一张图片。一个字符串类型的键允许存储的最大容量是512M。

Redis入门指南之三(入门)

1)设置与获取

  • set(name, value, ex=None, px=None, nx=False, xx=False)
Set the value at key ``name`` to ``value``
``ex`` sets an expire flag on key ``name`` for ``ex`` seconds.
``px`` sets an expire flag on key ``name`` for ``px`` milliseconds.
``nx`` if set to True, set the value at key ``name`` to ``value`` only if it does not exist.
``xx`` if set to True, set the value at key ``name`` to ``value`` only if it already exists.

这里只演示ex,其他参数类似,ex过期时间(秒) 这里过期时间是5秒,5秒后,键subject的值就变成None

 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
#r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.set('subject', 'Go', ex=)
print(r.get('subject'))

ex参数

对于上面的参数有下面的对应API可以直接设置:

setnx(name, value)
"Set the value of key ``name`` to ``value`` if key doesn't exist" setex(name, time, value)
Set the value of key ``name`` to ``value`` that expires in ``time`` seconds. ``time`` can be represented by an integer or a Python timedelta
object. psetex(name, time_ms, value)
Set the value of key ``name`` to ``value`` that expires in ``time_ms`` milliseconds. ``time_ms`` can be represented by an integer or a Python
timedelta object
  • mset(mapping)
Sets key/values based on a mapping. Mapping is a dictionary of key/value pairs. Both keys and values should be strings or types that can be 
cast to a string via str().
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.mset({"k1":"v1", "k2":"v2"})
print(r.mget('k1', 'k2')) #['v1', 'v2']

mset

  • mget(keys, *args)
Returns a list of values ordered identically to ``keys``
 import redis

 pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
print(r.mget('k1', 'k2')) # ['v1', 'v2']
print(r.mget(['k1', 'k2'])) # ['v1', 'v2']
print(r.mget("key1", "key2", "key3", "k1", "k2")) # [None, None, None, 'v1', 'v2']

mget

  • getset(name, value)
Sets the value at key ``name`` to ``value`` and returns the old value at key ``name`` atomically.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.set("subject", "Go")
print(r.get("subject")) # Go
print(r.getset("subject", "C++")) # Go
print(r.get("subject")) # C++

getset

  • getrange(key, start, end)
Returns the substring of the string value stored at ``key``, determined by the offsets ``start`` and ``end`` (both are inclusive)
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.set("subject", "Go语言")
print(r.getrange("subject", , )) # Go
print(r.getrange("subject", , )) # Go语 注意:汉字占三个字节,取的时候不能截断,比如取0,3错误
r.set("subject","C++") # 字母
print(r.getrange("subject", , )) # C++
print(r.getrange("subject", , -)) # C++

getrange

  • setrange(name, offset, value)
 Overwrite bytes in the value of ``name`` starting at ``offset`` with ``value``. If ``offset`` plus the length of ``value`` exceeds the length 
of the original value, the new value will be larger than before. If ``offset`` exceeds the length of the original value, null bytes will be
used to pad between the end of the previous value and the start of what's being injected. Returns the length of the new string.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.set("subject", "Go语言")
print(r.setrange("subject", , "C+")) # 注意:汉字三个占字节,汉字的字节位置是固定的,占了就报错
print(r.get("subject")) # C+语言 print(r.setrange("subject", , "hello你好")) # 设置成 也会报错
print(r.get("subject")) # C+语言hello你好

setrange

2)位操作

一个字节由8个二进制位组成,Redis提供了4个命令可以直接对二进制位进行操作。为了演示,先将foo键赋值为bar:

  • setbit(name, offset, value)
Flag the ``offset`` in ``name`` as ``value``. Returns a boolean indicating the previous value of ``offset``.

注意:值 value 只能是 1 或 0。

bar 的3个字母 'b','a','r'对应的ASCII码分别为98、97和114,转换成二进制分别如下:

Redis入门指南之三(入门)

 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.set("foo", "bar") # b a r
r.setbit("foo", , )
print(r.get("foo")) # car

setbit

  • getbit(name, offset)
Returns a boolean indicating the value of ``offset`` in ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.set("foo", "bar") # b a r
print(r.getbit("foo", )) #
r.setbit("foo", , )
print(r.getbit("foo", )) #
print(r.get("foo")) # car

getbit

  • bitcount(key, start=None, end=None)
Returns the count of set bits in the value of ``key``.  Optional ``start`` and ``end`` paramaters indicate which bytes to consider
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.set("foo", "bar") # b a r
print(r.bitcount("foo")) # "bar"转换成二进制总共有10个1

bitcount

  • bitop(operation, dest, *keys)
Perform a bitwise operation using ``operation`` between ``keys`` and store the result in ``dest``.
operation,AND(并) 、 OR(或) 、 NOT(非) 、 XOR(异或)
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.mset({"num1":"a", "num2":"b"})
print(r.get("num1"))
print(r.get("num2")) print(bin())
print(bin())
# a
# b r.bitop("AND", "kand", "num1", "num2")
print(r.get("kand")) r.bitop("OR", "kor", "num1", "num2")
print(r.get("kor")) r.bitop("XOR", "kxor", "num1", "num2")
print(r.get("kxor"))

bitop

3)strlen(name)

Return the number of bytes stored in the value of ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
r.set("foo", "helloworld")
print(r.strlen("foo")) #

strlen

4)incr(self, name, amount=1)

Increments the value of ``key`` by ``amount``.  If no key exists, the value will be initialized as ``amount``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
print(r.get("num")) # 之前存在为
r.incr("num", ) # + =
print(r.get("num")) print(r.get("newnum")) # redis实例中没有键 newnum
r.incr("newnum", ) # 初始化为
print(r.get("newnum")) #

incr

    应用场景 – 统计某篇博客或者某个网站一天的访问量:

假定我们对一系列页面需要记录点击次数。例如论坛的每个帖子都要记录点击次数,而点击次数比回帖的次数的多得多。如果使用关系数据库来存储点击,可能存在大量的行级锁争用。所以,点击数的增加使用redis的INCR命令最好不过了。当redis服务器启动时,可以从关系数据库读入点击数的初始值(12306这个页面被访问了666666次)。

r.set("visit:12306:totals", )
print(r.get("visit:12306:totals"))

每当有一个页面点击,则使用INCR增加点击数即可。

r.incr("visit:12306:totals")

页面载入的时候则可直接获取这个值。

print(r.get("visit:12306:totals"))

incrbyfloat(self, name, amount=1.0) 使用方法同 incr。

Increments the value at key ``name`` by floating ``amount``. If no key exists, the value will be initialized as ``amount``
decr(name, amount=1) 使用方法同 incr。
Decrements the value of ``key`` by ``amount``.  If no key exists, the value will be initialized as  - ``amount``
# An alias for ``decr()``, because it is already implemented
# as DECRBY redis command.

5)append(key, value)

Appends the string ``value`` to the value at ``key``. If ``key`` doesn't already exist, create it with a value of ``value``. Returns the new
length of the value at ``key``.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True) r = redis.Redis(connection_pool=pool)
print(r.get("name")) # wangwuzhangsan
r.append("name", "-zhangsan")
print(r.get("name")) # wangwuzhangsan-zhangsan
r.append("name", "-lisi")
print(r.get("name")) # wangwuzhangsan-zhangsan-lisi

append

(2) Hash 操作

redis中Hash在内存中的存储格式如下图:

Redis入门指南之三(入门)

1)设置与获取

  • hset(self, name, key, value)
Set ``key`` to ``value`` within hash ``name``. Returns  if HSET created a new field, otherwise hsetnx(self, name, key, value)
  • hsetnx(self, name, key, value)
Set ``key`` to ``value`` within hash ``name`` if ``key`` does not exist.  Returns  if HSETNX created a field, otherwise .
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hset("hash1", "k1", "v1")
r.hset("hash1", "k2", "v2")
print(r.hkeys("hash1")) # 取hash中所有的key ['k1', 'k2']
print(r.hget("hash1", "k1")) # 单个取hash的key对应的值 v1
print(r.hmget("hash1", "k1", "k2")) # 多个取hash的key对应的值 ['v1', 'v2']
r.hsetnx("hash1", "k2", "v3") # 只能新建
print(r.hget("hash1", "k2")) # 并没有修改 k2的值

hset

  • hmset(name, mapping)
Set key to value within hash ``name`` for each corresponding key and value from the ``mapping`` dict.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": "v1", "k2": "v2", "k3": "v3"})
print(r.hkeys("hash2")) # 取hash中所有的key ['k1', 'k3', 'k2']
print(r.hget("hash2", "k1")) # 单个取hash的key对应的值 v1
print(r.hmget("hash2", "k1", "k2", "k3")) # 多个取hash的key对应的值 ['v1', 'v2', 'v3']

hmset

  • hget(name, key)
Return the value of ``key`` within the hash ``name``
  • hmget(name, keys, *args)
Returns a list of values ordered identically to ``keys``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": "v1", "k2": "v2", "k3": "v3"})
print(r.hkeys("hash2")) # 取hash中所有的key ['k1', 'k3', 'k2']
print(r.hget("hash2", "k1")) # 单个取hash的key对应的值 v1
# 方式1
print(r.hmget("hash2", "k1", "k2", "k3")) # 多个取hash的key对应的值 ['v1', 'v2', 'v3']
# 方式2
print(r.hmget("hash2", ["k1", "k2", "k3"])) # 多个取hash的key对应的值 ['v1', 'v2', 'v3']

hmget

  • hgetall(name)
Return a Python dict of the hash's name/value pairs
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": "v1", "k2": "v2", "k3": "v3"})
print(r.hgetall("hash2")) # {'k2': 'v2', 'k1': 'v1', 'k3': 'v3'}

hgetall

  • hlen(name)
Return the number of elements in hash ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": "v1", "k2": "v2", "k3": "v3"})
print(r.hlen("hash2")) # 获取键值对的数量

hlen

  • hkeys(name)
Return the list of keys within hash ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": "v1", "k2": "v2", "k3": "v3"})
print(r.hkeys("hash2")) # ['k1', 'k3', 'k2']

hkeys

  • hvals(name)
Return the list of values within hash ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": "v1", "k2": "v2", "k3": "v3"})
print(r.hkeys("hash2")) # ['k1', 'k3', 'k2']
print(r.hvals("hash2")) # ['v1', 'v3', 'v2']

hvals

2)hexists(name, key)

Returns a boolean indicating if ``key`` exists within hash ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": "v1", "k2": "v2", "k3": "v3"})
print(r.hexists("hash2", "k1")) # True
print(r.hexists("hash2", "k5")) # False

hexists

3)hdel(name,*keys)

Delete ``keys`` from hash ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": "v1", "k2": "v2", "k3": "v3", "k5": "v5", "k6": "v6"})
print(r.hgetall("hash2")) # {'k5': 'v5', 'k2': 'v2', 'k1': 'v1', 'k6': 'v6', 'k3': 'v3'} r.hdel("hash2", "k1")
print(r.hgetall("hash2")) # {'k5': 'v5', 'k2': 'v2', 'k6': 'v6', 'k3': 'v3'} r.hdel("hash2", "k2", "k3")
print(r.hgetall("hash2")) # {'k6': 'v6', 'k5': 'v5'}

hdel

4)hincrby(name, key, amount=1)

Increment the value of ``key`` in hash ``name`` by ``amount``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": , "k2": , "k3": })
print(r.hget("hash2", "k1")) # r.hincrby("hash2", "k1", amount=)
print(r.hget("hash2", "k1")) #

hincrby

  • hincrbyfloat(name, key, amount=1.0)
Increment the value of ``key`` in hash ``name`` by floating ``amount``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.hmset("hash2", {"k1": , "k2": , "k3": })
print(r.hget("hash2", "k1")) # r.hincrbyfloat("hash2", "k1", amount=100.02)
print(r.hget("hash2", "k1")) # 101.02

hincrbyfloat

5)hscan(name, cursor=0, match=None, count=None)

name,redis的name
cursor,游标(基于游标分批取获取数据)
match,匹配指定key,默认None 表示所有的key
count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数
如:
第一次:cursor1, data1 = r.hscan(‘xx’, cursor=, match=None, count=None)
第二次:cursor2, data1 = r.hscan(‘xx’, cursor=cursor1, match=None, count=None)

直到返回值cursor的值为0时,表示数据已经通过分片获取完毕
Incrementally return key/value slices in a hash. Also return a cursor indicating the scan position.
``match`` allows for filtering the keys by pattern
``count`` allows for hint the minimum number of returns
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) dic = {}
for i in range():
key = "k%s" % i
value = "v%s" % i
dic[key] = value
r.hmset("hash2", dic) data = r.hscan("hash2")
while True:
if data[] != :
print(data)
data = r.hscan("hash2", cursor=data[])
else:
print(data)
break

hscan

注意:hscan它们每次执行都只会返回少量元素,所以该命令可以用于生产环境,而不会出现像 KEYS 或者 SMEMBERS 命令一次性将数据全部获取完,从而可能会将内存撑爆,带来的会阻塞服务器的问题。

更加详细的解释:http://www.redis.cn/commands/scan.html

6)hscan_iter(name, match=None, count=None)

利用yield封装hscan创建生成器,实现分批去redis中获取数据
参数:
match,匹配指定key,默认None 表示所有的key
count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数
ake an iterator using the HSCAN command so that the client doesn't need to remember the cursor position.
``match`` allows for filtering the keys by pattern
``count`` allows for hint the minimum number of returns
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) dic = {}
for i in range():
key = "k%s" % i
value = "v%s" % i
dic[key] = value
r.hmset("hash2", dic) for item in r.hscan_iter('hash2', count=):
print(item)
print(r.hscan_iter("hash2")) # 生成器内存地址

hscan_iter

(3)List 操作

1)lpush(name, *values)

每次将数据加入到key的头部。

Push ``values`` onto the head of the list ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) # lpush 将数据插入到头部
r.lpush("list1", , , ) # ['', '', '']
print(r.lrange('list1', , -)) r.lpush("list1", "newdata")
print(r.lrange('list1', , -)) # ['newdata', '', '', '']

lpush

  • rpush(name, *values)
Push ``values`` onto the tail of the list ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) # lpush 将数据插入到头部
r.lpush("list1", , , ) # ['', '', '']
print(r.lrange('list1', , -)) r.lpush("list1", "head")
print(r.lrange('list1', , -)) # ['head', '', '', ''] r.rpush("list1", "tail")
print(r.lrange('list1', , -)) # ['head', '', '', '', 'tail']

rpush

  • lpushx(name, value)
Push ``value`` onto the head of the list ``name`` if ``name`` exists
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.lpushx("list2", "head")
print(r.lrange('list2', , -)) # [] # lpush 将数据插入到头部
r.lpush("list2", , , ) # ['', '', '']
print(r.lrange('list2', , -)) r.lpushx("list2", "head")
print(r.lrange('list2', , -)) # ['head', '', '', '']

lpushx

  • rpushx(name, value)
Push ``value`` onto the tail of the list ``name`` if ``name`` exists

2)linsert(name, where, refvalue, value))

Insert ``value`` in list ``name`` either immediately before or after [``where``] ``refvalue``
Returns the new length of the list on success or - if ``refvalue`` is not in the list.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.rpush("list3", , , )
print(r.lrange('list3', , -)) # ['', '', ''] r.linsert("list3", "before", , "")
print(r.lrange('list3', , -)) # ['', '', '', ''] r.linsert("list3", "after", , "")
print(r.lrange('list3', , -)) # ['', '', '', '', '']

linsert

3)lset(name, index, value)

Set ``position`` of list ``name`` to ``value``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.rpush("list5", , , )
print(r.lrange('list5', , -)) # ['', '', ''] r.lset("list5", , )
print(r.lrange('list5', , -)) # 修改索引0处的值 -> ['', '', '']

lset

4)lrem(name, count, value)

Remove the first ``count`` occurrences of elements equal to ``value`` from the list stored at ``name``.
The count argument influences the operation in the following ways:
count > : Remove elements equal to value moving from head to tail.
count < : Remove elements equal to value moving from tail to head.
count = : Remove all elements equal to value.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.rpush("list5", , , , , ,)
print(r.lrange('list5', , -)) # ['', '', '', '', '', ''] # 如果 count > list中元素的个数,则删除list中所有该元素
# count <
r.lrem("list5", -, ) # from tail to head to delete 删除最先出现的11,删除一次
print(r.lrange('list5', , -)) # ['', '', '', '', '']
# count >
r.lrem("list5", , ) # from tail to head to delete 删除最先出现的11,删除一次
print(r.lrange('list5', , -)) # ['', '', '', '', '']
# count =
r.lrem("list5", , ) # from tail to head to delete 删除最先出现的11,删除一次
print(r.lrange('list5', , -)) # ['']

lrem

5)lpop(name)

Remove and return the first item of the list ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.rpush("list5", , , , , ,)
print(r.lrange('list5', , -)) # ['', '', '', '', '', ''] print(r.lpop("list5")) # 第0个
print(r.lrange('list5', , -)) # ['', '', '', '', '']
print(r.rpop("list5")) # 第4个
print(r.lrange('list5', , -)) # ['', '', '', '']

lpop

  • rpop(name)
Remove and return the last item of the list ``name``

6)ltrim(name, start, end)

Trim the list ``name``, removing all values not within the slice between ``start`` and ``end``
``start`` and ``end`` can be negative numbers just like Python slicing notation
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.rpush("list5", , , , , ,)
print(r.lrange('list5', , -)) # ['', '', '', '', '', ''] r.ltrim("list5", , -) # 删除两头的值
print(r.lrange('list5', , -)) # ['', '', '', '']

ltrim

7)lindex(name, index)

Return the item from list ``name`` at position ``index``
Negative indexes are supported and will return an item at the end of the list
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.rpush("list5", , , , , ,)
data = r.lrange("list5", , -)
print(data)
for i in range(len(data)):
print(i, r.lindex('list5', i))

index

8)rpoplpush(src, dst)

RPOP a value off of the ``src`` list and atomically LPUSH it on to the ``dst`` list.  Returns the value.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.rpush("list5", , , )
print(r.lrange("list5", , -)) r.rpush("list6", , , )
print(r.lrange("list6", , -))
# 注意一次只能从list5 的tail移动一个元素到list6的head
# r.rpoplpush("list5", "list6")
# print(r.lrange("list5", , -)) # ['', '']
# print(r.lrange("list6", , -)) # ['', '', '', ''] # 将list5 的所有元素移动一个list6中
src_len = len(r.lrange("list5", , -))
for io in range(src_len):
r.rpoplpush("list5", "list6")
print(r.lrange("list5", , -))
print(r.lrange("list6", , -))

rpoplpush

  • brpoplpush(src, dst, timeout=0)

注意:timeout  timeout,超时时间,当元素所有列表的元素获取完之后,阻塞等待列表内有数据的时间(秒), 0 表示永远阻塞

Pop a value off the tail of ``src``, push it on the head of ``dst`` and then return it.
This command blocks until a value is in ``src`` or until ``timeout`` seconds elapse, whichever is first. A ``timeout`` value of blocks
forever.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.rpush("list5", , , )
print(r.lrange("list5", , -)) r.rpush("list6", , , )
print(r.lrange("list6", , -))
# 注意一次只能从list5 的tail移动一个元素到list6的head
# r.brpoplpush("list5", "list6", timeout=)
# print(r.lrange("list5", , -)) # ['', '']
# print(r.lrange("list6", , -)) # ['', '', '', ''] # 将list5 的所有元素移动一个list6中
src_len = len(r.lrange("list5", , -))
for io in range(src_len):
r.brpoplpush("list5", "list6", timeout=)
print(r.lrange("list5", , -))
print(r.lrange("list6", , -)) print("done")

brpoplpush

  • brpop(keys, timeout)

从第一个list开始,依次循环从每个列表的tail取数据,直到所有list为空。

LPOP a value off of the first non-empty list named in the ``keys`` list.
If none of the lists in ``keys`` has a value to LPOP, then block for ``timeout`` seconds, or until a value gets pushed on to one of the lists.
If timeout is , then block indefinitely.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.rpush("list10", , , )
r.rpush("list11", , , ) list10_len = r.llen("list10")
list11_len = r.llen("list11") while (list10_len != or list11_len != ):
data = r.blpop(["list10", "list11"], timeout=)
print(data, r.lrange("list10", , -), r.lrange("list11", , -)) list10_len = r.llen("list10")
list11_len = r.llen("list11") # 从第一个list开始,依次循环从每个列表的tail取数据,直到所有list为空
# 执行结果:
# ('list10', '') ['', ''] ['', '', '']
# ('list10', '') [''] ['', '', '']
# ('list10', '') [] ['', '', '']
# ('list11', '') [] ['', '']
# ('list11', '') [] ['']
# ('list11', '') [] []

blpop

注意:可以通 r.llen(name) 获取list的长度。

9)自定义增量迭代

由于redis类库中没有提供对列表元素的增量迭代,如果想要循环name对应的列表的所有元素,那么就需要:
)获取name对应的所有列表
)循环列表
但是,如果列表非常大,那么就有可能在第一步时就将程序的内存撑爆,所有有必要自定义一个增量迭代的功能:
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) def list_iter(name):
"""
自定义redis列表增量迭代
:param name: redis中的name,即:迭代name对应的列表
:return: yield 返回 列表元素
"""
list_len = r.llen(name)
for index in range(list_len):
yield r.lindex(name, index) print(r.lrange("list2", , -)) # ['head', '', '', '']
# 使用
for item in list_iter('list2'): # 遍历这个列表
print(item)

自定义增量迭代

(4)Set 操作

Set操作,Set集合就是不允许重复的列表,本身是无序的。

1)sadd(name, *values)

Add ``value(s)`` to set ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sadd("set1", , , , ) # 往集合中添加元素
print(r.scard("set1")) # 集合的长度是4
print(r.smembers("set1")) # 获取集合中所有的成员 {'', '', '', ''} r.sadd("set1", , ) # 往集合中添加元素 会自动去重
print(r.scard("set1")) # 集合的长度是4
print(r.smembers("set1")) # 获取集合中所有的成员 {'', '', '', '', ''}

sadd

2)scard(name)

Return the number of elements in set ``name``

3)smembers(name)

Return all members of the set ``name``
  • sscan(name, cursor=0, match=None, count=None)
Incrementally return lists of elements in a set. Also return a cursor indicating the scan position.
``match`` allows for filtering the keys by pattern
``count`` allows for hint the minimum number of returns
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) print(r.smembers("set1")) for i in range():
r.sadd("set1", i) # 往集合中添加元素 data = r.sscan("set1") # 返回的是一个元组,第一个是游标,供下次使用,第二个是数据
print(data) while data[]:
data = r.sscan("set1", cursor=data[])
print(data)

sscan

类似于 hscan,可以参考学习。

  • sscan_iter(name, match=None, count=None)
Make an iterator using the SSCAN command so that the client doesn't need to remember the cursor position.
``match`` allows for filtering the keys by pattern
``count`` allows for hint the minimum number of returns
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) print(r.smembers("set1")) for i in range():
r.sadd("set1", i) # 往集合中添加元素 print(r.sscan_iter("set1")) # <generator object Redis.sscan_iter at 0x0000000002C49DB0> for data in r.sscan_iter("set1"):
print(data)

sscan_iter

注意:同字符串的操作,用于增量迭代分批获取元素,避免内存消耗太大。

4)sdiff(keys, *args)

集合求差集:在第一个name对应的集合中且不在其他name对应的集合的元素集合

Return the difference of sets specified by ``keys``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sadd("set1", , , )
r.sadd("set2", , , )
print(r.smembers("set1")) # 获取集合中所有的成员
print(r.smembers("set2")) print(r.sdiff("set1", "set2")) # 在集合set1但是不在集合set2中 {''}
print(r.sdiff("set2", "set1")) # 在集合set2但是不在集合set1中 {''}

sdiff

5)sdiffstore(dest, keys, *args)

将在第一个name对应的集合中且不在其他name对应的集合中的元素加入到dest对应的集合中。

Store the difference of sets specified by ``keys`` into a new set named ``dest``.  
Returns the number of keys in the new set.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sadd("set1", , , , )
r.sadd("set2", , , )
print(r.smembers("set1"))
print(r.smembers("set2")) r.sdiffstore("set3", "set1", "set2")
print(r.smembers("set3")) # {'', ''} , 和 55在set1中不在set2中

sdiffstore

6)sinter(keys, *args)

获取交集。

Return the intersection of sets specified by ``keys``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sinter("set1", , , , , )
r.sadd("set2", , , )
print(r.smembers("set1"))
print(r.smembers("set2")) print(r.sinter("set1", "set2")) # {'', ''}

sinter

  • sinterstore(dest, keys, *args)
Store the intersection of sets specified by ``keys`` into a new set named ``dest``.  Returns the number of keys in the new set.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sinter("set1", , , , , )
r.sadd("set2", , , )
print(r.smembers("set1"))
print(r.smembers("set2")) print(r.sinterstore("set3", "set1", "set2")) # {'', ''}
print(r.smembers("set3")) # {'', ''}

sinterstore

7)sunion(keys, *args)

Return the union of sets specified by ``keys``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sinter("set1", , , , , )
r.sadd("set2", , , )
print(r.smembers("set1"))
print(r.smembers("set2")) print(r.sunion("set1", "set2")) # {'', '', '', '', ''}

sunion

  • sunionstore(dest,keys, *args)
Store the union of sets specified by ``keys`` into a new  set named ``dest``.  Returns the number of keys in the new set.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sinter("set1", , , , , )
r.sadd("set2", , , )
print(r.smembers("set1"))
print(r.smembers("set2")) print(r.sunionstore("set3", "set1", "set2"))
print(r.smembers("set3")) # {'', '', '', '', ''}

sunionstore

8)sismember(name, value)

检查value是否是name对应的集合的成员,结果为True和False

Return a boolean indicating if ``value`` is a member of set ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sinter("set1", , , , , )
r.sadd("set2", , , )
print(r.smembers("set1"))
print(r.smembers("set2")) print(r.sismember("set1", )) # True
print(r.sismember("set2", )) # False

sismember

9)smove(src, dst, value)

将 src 中的成员移动到 dst 集合中。

Move ``value`` from set ``src`` to set ``dst`` atomically
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sadd("set1", , , , , )
r.sadd("set2", , , )
print(r.smembers("set1"))
print(r.smembers("set2")) r.smove("set1", "set3", )
print(r.smembers("set1")) # {'', '', '', ''} 将 set1中的11移到set3中
print(r.smembers("set3")) # {''}

smove

10)spop(name)

从集合移除一个成员,并将其返回,说明一下,集合是无序的,所有是随机删除的。

Remove and return a random member of set ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sadd("set1", , , , , )
r.sadd("set2", , , )
print(r.smembers("set1"))
print(r.smembers("set2")) print(r.spop("set1")) #
print(r.spop("set2")) #

spop

11)srem(name, values)

从集合name中删除指定的元素。

Remove ``values`` from set ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.sadd("set1", , , , , )
r.sadd("set2", , , )
print(r.smembers("set1"))
print(r.smembers("set2")) r.srem("set1", , )
print(r.smembers("set1")) # {'', '', ''}
r.srem("set2", , )
print(r.smembers("set2")) # {''}

srem

(5)Sort Set 操作

Set操作,Set集合就是不允许重复的列表,本身是无序的。有序集合,在集合的基础上,为每元素排序;元素的排序需要根据另外一个值来进行比较,所以,对于有序集合,每一个元素有两个值,即:值和分数,分数专门用来做排序。

1)zadd(name, mapping, nx=False, xx=False, ch=False, incr=False)

Set any number of element-name, score pairs to the key ``name``. Pairs are specified as a dict of element-names keys to score values.
``nx`` forces ZADD to only create new elements and not to update scores for elements that already exist. ``xx`` forces ZADD to only update scores of elements that already exist. New elements will not be added. ``ch`` modifies the return value to be the numbers of elements changed. Changed elements include new elements that were added and elements
whose scores changed. ``incr`` modifies ZADD to behave like ZINCRBY. In this mode only a single element/score pair can be specified and the score is the amount
the existing score will be incremented by. When using this mode the return value of ZADD will be the new score of the element. The return value of ZADD varies based on the mode specified. With no options, ZADD returns the number of new elements added to the sorted
set.
ZADD 参数(options) (>= Redis 3.0.)
ZADD 命令在key后面分数/成员(score/member)对前面支持一些参数,他们是:
XX: 仅仅更新存在的成员,不添加新成员。
NX: 不更新存在的成员。只添加新成员。
CH: 修改返回值为发生变化的成员总数,原始是返回新添加成员的总数 (CH 是 changed 的意思)。更改的元素是新添加的成员,已经存在的成员更新分数。 所以在命令中指定的成员有相同
的分数将不被计算在内。注:在通常情况下,ZADD返回值只计算新添加成员的数量。
INCR: 当ZADD指定这个选项时,成员的操作就等同ZINCRBY命令,对成员的分数进行递增操作。
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.zadd("zset1", {"n1":, "n2":})
r.zadd("zset2", {'m1':, 'm2':})
print(r.zcard("zset1")) # 集合长度
print(r.zcard("zset2")) # 集合长度
print(r.zrange("zset1", , -)) # 获取有序集合中所有元素 ['n1', 'n2']
print(r.zrange("zset2", , -, withscores=True)) # 获取有序集合中所有元素和分数 [('m1', 22.0), ('m2', 44.0)]

zadd

2)zcard(name)

获取有序集合元素个数 类似于len

Return the number of elements in the sorted set ``name``

3)zrange( name, start, end, desc=False, withscores=False, score_cast_func=float)

Return a range of values from sorted set ``name`` between
``start`` and ``end`` sorted in ascending order.
``start`` and ``end`` can be negative, indicating the end of the range.
``desc`` a boolean indicating whether to sort the results descendingly
``withscores`` indicates to return the scores along with the values. The return type is a list of (value, score) pairs
``score_cast_func`` a callable used to cast the score return value
  • zrevrange(name, start, end, withscores=False, score_cast_func=float)

从大到小排序(同zrange,集合是从大到小排序的)

Return a range of values from sorted set ``name`` between
``start`` and ``end`` sorted in descending order.
``start`` and ``end`` can be negative, indicating the end of the range.
``withscores`` indicates to return the scores along with the values The return type is a list of (value, score) pairs
``score_cast_func`` a callable used to cast the score return value
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.zadd("zset1", {"n1":, "n2":})
r.zadd("zset2", {'m1':, 'm2':})
print(r.zcard("zset1")) # 集合长度
print(r.zcard("zset2")) # 集合长度
print(r.zrevrange("zset1", , -)) # ['n2', 'n1']
print(r.zrevrange("zset2", , -, withscores=True)) # [('m2', 44.0), ('m1', 22.0)]

zrevrange

  • zrangebyscore(name, min, max, start=None, num=None, withscores=False, score_cast_func=float)

按照分数范围获取name对应的有序集合的元素。

Return a range of values from the sorted set ``name`` with scores
between ``min`` and ``max``.
If ``start`` and ``num`` are specified, then return a slice
of the range.
``withscores`` indicates to return the scores along with the values.
The return type is a list of (value, score) pairs
`score_cast_func`` a callable used to cast the score return value
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) for i in range(, ):
element = 'n' + str(i)
r.zadd("zset3", {element:i})
print(r.zrangebyscore("zset3", , )) # # 在分数是15-25之间,取出符合条件的元素
print(r.zrangebyscore("zset3", , , withscores=True)) # 在分数是12-22之间,取出符合条件的元素(带分数)

zrangebyscore

  • zrevrangebyscore(name, max, min, start=None, num=None, withscores=False, score_cast_func=float)

同zrangebyscore,只是以降序排列

 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) for i in range(, ):
element = 'n' + str(i)
r.zadd("zset3", {element:i})
print(r.zrevrangebyscore("zset3", , , withscores=True)) # 在分数是22-11之间,取出符合条件的元素 按照分数降序排列

zrevrangebyscore

  • zscan(name, cursor=0, match=None, count=None, score_cast_func=float)

类似于 hscan,可以参考学习

 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) for i in range(, ):
element = 'n' + str(i)
r.zadd("zset3", {element:i}) data = r.zscan("zset3", cursor=)
print(data) while data[]:
data = r.zscan("zset3", cursor=data[])
print(data)

zscan

  • zscan_iter(name, match=None, count=None,score_cast_func=float)

获取所有元素–迭代器

 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) for i in range(, ):
element = 'n' + str(i)
r.zadd("zset3", {element:i}) for i in r.zscan_iter("zset3"):
print(i)

zscan_iter

  • zcount(name, min, max)

获取name对应的有序集合中分数 在 [min,max] 之间的个数

Returns the number of elements in the sorted set at key ``name`` with a score between ``min`` and ``max``.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) for i in range(, ):
element = 'n' + str(i)
r.zadd("zset3", {element:i}) print(r.zrange("zset3", , -, withscores=True))
print(r.zcount("zset3", , )) #

zcount

4)zincrby(name, amount, value)

Increment the score of ``value`` in sorted set ``name`` by ``amount``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.zadd("zset3", {"num":}) r.zincrby("zset3", , "num") # 将num的分数自增20
print(r.zrange("zset3", , -, withscores=True)) # [('num', 20.0)]

zincrby

5)zrank(name, value)

获取某个值在 name对应的有序集合中的索引(从 0 开始)

Returns a -based value indicating the rank of ``value`` in sorted set ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.zadd("zset3", {"n1":, "n3":, "n2":, "n6":, "n5":}) print(r.zrank("zset3", "n1")) # n1的索引号是0 这里按照分数顺序(从小到大)
print(r.zrank("zset3", "n6")) # n6的索引号是4 print(r.zrevrank("zset3", "n1")) # n1的索引号是4 这里安照分数倒序(从大到小)

zrank

  • zrevrank(name, value)

6)zrem(name, *values)

Remove member ``values`` from sorted set ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.zadd("zset3", {"n1":, "n3":, "n2":, "n6":, "n5":}) r.zrem("zset3", "n1", "n6") #删除集合中的n1 和 n6
print(r.zrange("zset3", , -)) # ['n2', 'n3', 'n5']

zrem

  • zremrangebyrank(name, min, max)
Remove all elements in the sorted set ``name`` with ranks between ``min`` and ``max``. Values are -based, ordered from smallest score to 
largest. Values can be negative indicating the highest scores. Returns the number of elements removed
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.zadd("zset3", {"n1":, "n3":, "n2":, "n6":, "n5":}) r.zremrangebyrank("zset3", , ) # 删除有序集合中的索引号是0, 1的元素
print(r.zrange("zset3", , -)) # ['n3', 'n5', 'n6']

zremrangebyrank

  • zremrangebyscore(name, min, max)
Remove all elements in the sorted set ``name`` with scores between ``min`` and ``max``. Returns the number of elements removed.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.zadd("zset3", {"n1":, "n3":, "n2":, "n6":, "n5":}) r.zremrangebyscore("zset3", , ) # 删除有序集合中的索引号是0, 1的元素
print(r.zrange("zset3", , -)) # ['n1', 'n6']

zremrangebyscore

7)zscore(name, value)

Return the score of element ``value`` in sorted set ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.zadd("zset3", {"n1":, "n3":, "n2":, "n6":, "n5":}) print(r.zscore("zset3", "n3")) # 30.0

zscore

(6)其他常用操作

1)delete(*names)

根据删除redis中的任意数据类型(string、hash、list、set、有序set)

Delete one or more keys specified by ``names``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.set("string", "strvalue")
r.hset("hset", "hsetkey", "hsetvalue")
r.lpush("list", "listvalue1", "listvalue2")
r.sadd("set", "setvalue1", "setvalue2")
r.zadd("zset", {"n1":, "n3":, "n2":, "n6":, "n5":}) del_key = ["string", "hset", "list", "set", "zset"] for key in r.keys():
if key in del_key:
r.delete(key)

delete

2)exists(name)

Returns the number of ``names`` that exist
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.set("string", "strvalue")
r.hset("hset", "hsetkey", "hsetvalue")
r.lpush("list", "listvalue1", "listvalue2")
r.sadd("set", "setvalue1", "setvalue2")
r.zadd("zset", {"n1":, "n3":, "n2":, "n6":, "n5":}) del_key = ["string", "hset", "list", "set", "zset"] for key in del_key:
if r.exists(key):
print(key, " exists")
r.delete(key) print("--------------") for key in del_key:
if not r.exists(key):
print(key, "do not exists")

exists

3)模糊匹配

参见 4.1 热身中的获得符合规则的键名列表

  • keys(pattern='*')
Returns a list of keys matching ``pattern``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.set("fo", "bar")
r.set("foo", "bar")
r.set("fao", "bar")
r.set("fdo", "bar") print(r.keys(pattern="*")) # 默认 ['zset', 'fdo', 'list', 'fo', 'set', 'fao', 'string', 'hset', 'foo']
print(r.keys(pattern="fo*")) # ['fo', 'foo']
print(r.keys(pattern="f?o")) # ['fao', 'foo', 'fdo']
print(r.keys(pattern="f[a-c]o")) # ['fao']

keys

4)expire(name ,time)

为某个redis的某个name设置超时时间

Set an expire flag on key ``name`` for ``time`` seconds. ``time`` can be represented by an integer or a Python timedelta object.
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis
import time pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.set("foo", "bar") r.expire("foo", ) # 5秒之后 foo将不存在 for i in range():
if r.exists("foo"):
print("foo", " exists")
else:
print("foo", " do not exists")
time.sleep()

expire

5)rename(src, dst)

Rename key ``src`` to ``dst``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.set("foo", "bar")
r.rename("foo", "foo_bak") print(r.get("foo")) # None
print(r.get("foo_bak")) # bar

rename

6)randomkey()

Returns the name of a random key
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.mset({"name":"nova", "age":, "sex":"male", "address":"xian"})
print(r.randomkey())

randomkey

7)type(name)

Returns the type of key ``name``
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.set("string", "strvalue")
r.hset("hset", "hsetkey", "hsetvalue")
r.lpush("list", "listvalue1", "listvalue2")
r.sadd("set", "setvalue1", "setvalue2")
r.zadd("zset", {"n1":, "n3":, "n2":, "n6":, "n5":}) del_key = ["string", "hset", "list", "set", "zset"] for key in del_key:
if r.exists(key):
print(key, r.type(key))

type

8)scan(cursor=0, match=None, count=None)

类似于 hscan,可以参考学习

9)scan_iter(match=None, count=None)

10)dbsize()

Returns the number of keys in the current database

11)save()

ell the Redis server to save its data to disk, blocking until the save is complete

12)flushdb(asynchronous=False)

Delete all keys in the current database.
``asynchronous`` indicates whether the operation is executed asynchronously by the server.

(4)管道(pipeline)

redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,
如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。 管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) # pipe = r.pipeline(transaction=False) # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。
# pipe = r.pipeline(transaction=True)
pipe = r.pipeline() # 创建一个管道 # pipe.set('name', 'jack')
# pipe.set('role', 'sb')
# pipe.incr('num') # 如果num不存在则vaule为1,如果存在,则value自增1
# pipe.execute() # 管道的命令可以写在一起,下面命令和上面等价 如:
pipe.set('name', 'jack').set('role', 'sbb').incr('num').execute() print(r.get("name"))
print(r.get("role"))
print(r.get("num"))

pipeline

 #!/usr/bin/env python
# -*- coding:utf- -*- import redis pool = redis.ConnectionPool(host='192.168.30.134', port=, decode_responses=True)
# r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, decode_responses=True, decode_responses=True)
r = redis.Redis(connection_pool=pool) r.set('count',) with r.pipeline() as pipe: # 先监视,自己的值没有被修改过
r.watch('count') # 事务开始
pipe.multi()
old_count = r.get('count')
count = int(old_count)
if count > : # 有库存
pipe.set('count', count - ) # 执行,把所有命令一次性推送过去
pipe.execute()
print(r.get("count")) #

计数

(5)发布订阅

Redis入门指南之三(入门)

  • 发布者:服务器
  • 订阅者:Dashboad和数据处理
 #!/usr/bin/env python
# -*- coding:utf- -*- import redis class RedisHelper: def __init__(self):
self.__conn = redis.Redis(host='192.168.30.134', port=, decode_responses=True)
self.chan_sub = 'fm104.5'
self.chan_pub = 'fm104.5' def public(self, msg):
self.__conn.publish(self.chan_pub, msg)
return True def subscribe(self):
pub = self.__conn.pubsub()
pub.subscribe(self.chan_sub)
pub.parse_response()
return pub

monitor.py

 #!/usr/bin/env python
# -*- coding:utf- -*- from monitor import RedisHelper obj = RedisHelper()
redis_sub = obj.subscribe() while True:
msg = redis_sub.parse_response()
print(msg) # ['message', 'fm104.5', 'hello anybody']

Subscribe.py

 #!/usr/bin/env python
# -*- coding:utf- -*- from monitor import RedisHelper obj = RedisHelper()
obj.public('hello anybody')

public.py

(6)sentinel

Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端)都没有实现自动进行主备切换,而Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能进行自动切换。
      它的主要功能有以下几点:

  • 不时地监控redis是否按照预期良好地运行;
  • 如果发现某个redis节点运行出现状况,能够通知另外一个进程(例如它的客户端);
  • 能够进行自动切换。当一个master节点不可用时,能够选举出master的多个slave(如果有超过一个slave的话)中的一个来作为新的master,其它的slave节点会将它所追随的master的地址改为被提升为master的slave的新地址。
 #!/usr/bin/env python
# -*- coding:utf- -*- from redis.sentinel import Sentinel # 连接哨兵服务器(主机名也可以用域名)
sentinel = Sentinel([('10.211.55.20', ),
('10.211.55.20', ),
],
socket_timeout=0.5) # # 获取主服务器地址
# master = sentinel.discover_master('mymaster')
# print(master)
#
# # # 获取从服务器地址
# slave = sentinel.discover_slaves('mymaster')
# print(slave)
#
#
# # # 获取主服务器进行写入
# master = sentinel.master_for('mymaster')
# master.set('foo', 'bar') # # # # 获取从服务器进行读取(默认是round-roubin)
# slave = sentinel.slave_for('mymaster', password='redis_auth_pass')
# r_ret = slave.get('foo')
# print(r_ret)

sentinel

参考文献:

  • http://www.cnblogs.com/wupeiqi/articles/5132791.html
  • Redis入门指南 (第二版)
  • https://www.cnblogs.com/LearningOnline/p/9456806.html (linux上部署redis实现与Python上的redis交互)
  • http://www.redis.cn/commands/scan.html
  • https://blog.csdn.net/kl28978113/article/details/78726946
  • https://segmentfault.com/a/1190000002680804 (Redis Sentinel机制与用法)
上一篇:Redis 小白指南(一)- 简介、安装、GUI 和 C# 驱动介绍


下一篇:226. Invert Binary Tree(C++)