一、模块导入

  • 安装第三方包redis,然后导入StrictRedis模块
1
2
# 导入模块
from redis import StrictRedis

二、创建StrictRedis对象

  • StrictRedis对象:⽤于连接redis服务器,并按照不同类型提供了不同⽅法,进⾏交互操作

  • 创建对象:指定参数与指定的服务器和端⼝连接

    • host:默认为localhost
    • port:默认为6379
    • db:默认为0
1
2
# 简写 sr=StrictRedis()
sr = StrictRedis(host='192.168.241.128', port=6379, db=0)

三、StrictRedis对象的实例方法

  • 根据不同的类型,拥有不同的实例⽅法可以调⽤,与redis命令对应,⽅法需要的参数与命令的参数⼀致
string keys hash list set zset
set exists hset lpush sadd zadd
setex type hmset rpush smembers zrange
mset delete hkeys linsert srem zrangebyscore
append expire hget lrange zscore
get getrange hmget lset zrem
mget ttl hvals lrem zremrangebyscore
key hdel

四、数据操作

1. 获取键

  • ⽅法keys,根据正则表达式获取键
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from redis import StrictRedis

if __name__=="__main__":
try:
# 创建StrictRedis对象,与redis服务器建⽴连接
sr = StrictRedis(host='192.168.241.128', port=6379, db=0)
# 获取所有的键
result = sr.keys()
# 输出响应结果,所有的键构成⼀个列表,如果没有键则返回空列表
print(result)
except Exception as e:
print(e)

#[b'name']

2. string

  • ⽅法set:添加键、值,如果添加成功则返回True,如果添加失败则返回False
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from redis import StrictRedis

if __name__=="__main__":
try:
# 创建StrictRedis对象,与redis服务器建⽴连接
sr = StrictRedis(host='192.168.241.128', port=6379, db=0)
# 添加键name,值为zhangsan
result = sr.set('name','zhangsan')
# 输出响应结果,如果添加成功则返回True,否则返回False
print(result)
except Exception as e:
print(e)

#True
  • ⽅法get:获取键对应的值,如果键存在则返回对应的值,如果键不存在则返回None
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from redis import StrictRedis

if __name__=="__main__":
try:
# 创建StrictRedis对象,与redis服务器建⽴连接
sr = StrictRedis(host='192.168.241.128', port=6379, db=0)
# 添加键name,值为zhangsan
result = sr.get('name')
# 输出键的值,如果键不存在则返回None
print(result)
except Exception as e:
print(e)

#b'zhangsan'
  • ⽅法set:如果键已经存在则进⾏修改,如果键不存在则进⾏添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from redis import StrictRedis

if __name__=="__main__":
try:
# 创建StrictRedis对象,与redis服务器建⽴连接
sr = StrictRedis(host='192.168.241.128', port=6379, db=0)
# 添加键name,值为zhangsan
result = sr.set('name','lisi')
# 输出响应结果,如果添加成功则返回True,否则返回False
print(result)
except Exception as e:
print(e)

#True
  • ⽅法delete:删除键及对应的值,如果删除成功则返回受影响的键数,否则则返回0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from redis import StrictRedis

if __name__=="__main__":
try:
# 创建StrictRedis对象,与redis服务器建⽴连接
sr = StrictRedis(host='192.168.241.128', port=6379, db=0)
# 删除键及对应的值,如果删除成功则返回受影响的键数,否则则返回0
result = sr.delete('name')
# 输出响应结果,如果添加成功则返回True,否则返回False
print(result)
except Exception as e:
print(e)

#1

五、连接集群

  • 安装第三方包redis-py-cluster,然后导入RedisCluster模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from rediscluster import RedisCluster
if __name__ == '__main__':

# 构建所有的节点,Redis会使⽤CRC16算法,将键和值写到某个节点上
startup_nodes = [
{'host': '192.168.241.128', 'port': '7000'},
{'host': '192.168.241.130', 'port': '7001'},
{'host': '192.168.241.128', 'port': '7002'},
]

try:
# 构建StrictRedisCluster对象
src=RedisCluster(startup_nodes=startup_nodes)
# 设置键为name、值为itheima的数据
result=src.set('name','anny')
print(result)
# 获取键为name
name = src.get('name')
print(name)
except Exception as e:
print(e)

# True
# b'anny'

附录

  • 第三方包源码地址

https://github.com/Grokzen/redis-py-cluster