1、安装
pip inastall redis
2、操作
# 链接数据池 import time import redis # 创建数据的连接池 pool = redis.ConnectionPool(host="localhost", port=6379) # 通过连接池创建一个数据库的连接 conn = redis.Redis(connection_pool=pool) # 操作string conn.set("name", "lida") print(conn.get("name")) # 操作list conn.lpush("a", "bb", "ccc", "dddd") time.sleep(1) print(conn.lrange("dddd", 0, -1)) # 操作set,sadd增加元素 conn.sadd("a", "bb", "ccc", "dddd") time.sleep(1) print(conn.smembers("dddd")) # 操作hash conn.hset("student", "name", "lida") time.sleep(1) print(conn.hget("student", "name")) # 操作zset conn.zadd("age", {"张三": 20, "李四": 23}) time.sleep(1) print(conn.zrangebyscore("age", 0, 1))