- 安装
pip install redis
import redis
pool = redis.ConnectionPool(host="xxxxxx", port=6382, db=0, password="xxxxxx", encoding="utf-8")
r = redis.Redis(connection_pool=pool)
# 字符串操作
ret = r.setnx("name", "马亚".encode())
print(ret)
name = r.get("name").decode()
print(name)
# 计数操作
ret = r.incr("counter", 1)
print(ret)
# 哈希操作
r.hset("stu01", "age", 5)
ret = r.hgetall("stu01")
for k, v in ret.items():
print(k.decode(), v.decode())
# 列表操作
r.delete("l01")
r.rpush("l01", 1, 2, 3)
ret = r.lrange("l01", 0, -1)
for v in ret:
print(v.decode())
# 集合操作
r.sadd("set01", 11, 22, 33, 11, 22)
ret = r.smembers("set01")
for v in ret:
print(v.decode())
# 有序集合操作
r.zadd("zset01", {"小张": 68, "xiaohong": 77, "xiaowang": 99})
ret = r.zrange("zset01", 0, -1, withscores=True)
for v in ret:
print(v[0].decode(), int(v[1]))
redis-py官方文档
redis客户端