1、下载 redis 模块
2、redis 数据库两种连接方式
import redis
re = redis.Redis(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True) # host 是 redis 主机,需要 redis 服务端和客户端都启动 redis 默认端口是 6379,password 是 redis 数据库访问密码,db 是库
value = re.get("Student1") # 取出键 Student 对应的值
print(value)
#结果如下
zhangsan
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.get("Student1"))
# 结果如下
zhangsan
3、redis 基本操作命令
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.get("Student1"))
# 结果如下
zhangsan
import redis
re = redis.Redis(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
value = re.mget("Student1","Student2") # 同时获取键 Student1 和 Student2 的 value 值
print(value)
# 结果如下
['zhangsan', 'lisi']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
name1 = re.get("Student1")
print(name1)
name2 = re.getrange("Student1",0,3) # 取出索引 [0,3] 所对应的序列
print(name2)
# 结果如下
zhaoliu
zhao
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.set("Student","Alex") # 设置 Student 值为 Alex
name = re.get("Student")
print(name)
# 结果如下
Alex
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.mset({"Student1":"zhangsan","Student2":"lisi"})
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.getset("Student1","zhaoliu")) # 将 Student1 对应的值更新为 zhaoliu,并且返回原来的值
# 结果如下
wangwu # 返回的原来的值
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
name1 = re.get("Student1")
print(name1)
re.append("Student1","liu") # 在 Student1 对应的值上追加字符串 liu
name2 = re.get("Student1")
print(name2)
# 结果如下
zhaoliu
zhaoliuliu
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
name = re.get("Student1")
w = re.strlen("Student1") # 获取 Student1 所对应的字符串长度
print(name)
print(w)
# 结果如下
zhaoliu
7
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
name = re.get("Student1")
w = re.strlen("Student1") # 获取 Student1 所对应的字符串长度
print(name)
print(w)
# 结果如下
赵六
6
import redis
re = redis.Redis(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
value = re.lindex("Fruits",2) # 通过索引值 2 取出对应的值 Apple
print(value)
# 结果如下
Apple
import redis
re = redis.Redis(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
value = re.lrange("Fruits",0,1) # 取出列表中索引值为 0 和 1 的 value 值
print(value)
# 结果如下
['Orange', 'Banana']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
num = re.llen("Fruits")
print(num)
# 结果如下
4
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.lpush("Fruits","Apple","Banana","Orange") # 在 Fruits 对应的 list 中添加 Apple,Banana,Orange 三个元素
li = re.lrange("Fruits",0,2)
print(li)
# 结果如下
['Orange', 'Banana', 'Apple'] # 保存在列表中的顺序与添加的顺序相反
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.rpush("Fruits","Apple","Banana","Orange") # 在 Fruits 对应的 list 中添加 Apple,Banana,Orange 三个元素
li = re.lrange("Fruits",0,2)
print(li)
# 结果如下
['Apple', 'Banana', 'Orange']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.lpushx("Fruits","Pear") # 在 Fruits 对应的 list 中添加 Pear 单个元素
li = re.lrange("Fruits",0,3)
print(li)
# 结果如下
['Pear', 'Apple', 'Banana', 'Orange']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.rpushx("Fruits","Pear") # 在 Fruits 对应的 list 中添加 Pear 单个元素
li = re.lrange("Fruits",0,3)
print(li)
# 结果如下
['Apple', 'Banana', 'Orange', 'Pear']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
li1 = re.lrange("Fruits",0,10)
print(li1)
re.linsert("Fruits","BEFORE","Banana","Grape") # 在列表 Fruits 中找到元素 Banana,在 Banana 前插入 Grape
li2 = re.lrange("Fruits",0,10)
print(li2)
# 结果如下
['Apple', 'Banana', 'Orange', 'Pear']
['Apple', 'Grape', 'Banana', 'Orange', 'Pear']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
li1 = re.lrange("Fruits",0,10)
print(li1)
re.lset("Fruits",1,"Pear") # 对 Fruits 中的索引位置为 1 的值进行修改成 Pear
li2 = re.lrange("Fruits",0,10)
print(li2)
# 结果如下
['Apple', 'Grape', 'Banana', 'Orange']
['Apple', 'Pear', 'Banana', 'Orange'] # Grape 更新为 Pear
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
li1 = re.lrange("Fruits",0,10)
print(li1)
fruit = re.lpop("Fruits") # 移除 Fruits 所对应列表中的第一个值,且将移除的值返回出来
print(fruit)
li2 = re.lrange("Fruits",0,10)
print(li2)
# 结果如下
['Apple', 'Orange', 'Pear', 'Banana']
Apple
['Orange', 'Pear', 'Banana']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
li1 = re.lrange("Fruits",0,10)
print(li1)
re.ltrim("Fruits",0,1) # 移除 [0,1] 索引值之外的所有值
li2 = re.lrange("Fruits",0,10)
print(li2)
# 结果如下
['Apple', 'Orange', 'Pear', 'Banana']
['Apple', 'Orange']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.hset("Teacher","Teacher01","Mr.ZHENG")
re.hset("Teacher","Teacher02","Mr.XIAO")
re.hset("Teacher","Teacher03","Mr.FU")
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
dic = {"Teacher01":"Mr.ZHENG","Teacher02":"Mr.XIAO","Teacher03":"Mr.FU",}
re.hmset("Teacher",dic) # 批量设置多个键值对
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
value = re.hget("Teacher","Teacher02") # 获取 Teacher 对应的 hash 中键 Teacher02 的 value 值
print(value)
# 结果如下
Mr.XIAO
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
dic = re.hgetall("Teacher") # 获取 Teacher 对应 hash 的所有键值对
print(dic)
print(type(dic))
# 结果如下
{'Teacher01': 'Mr.ZHENG', 'Teacher02': 'Mr.XIAO', 'Teacher03': 'Mr.FU'}
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
keys = re.hkeys("Teacher") # 获取 Teacher 所对应 hash 中的所有的 key
print(keys)
# 结果如下
['Teacher01', 'Teacher02', 'Teacher03']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
values = re.hvals("Teacher") # 获取 Teacher 所对应 hash 中的所有的 value
print(values)
# 结果如下
['Mr.ZHENG', 'Mr.XIAO', 'Mr.FU']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
keys = re.hmget("Teacher","Teacher01","Teacher02") #在 Teacher 对应的 hash 中获取 Teacher01 和 Teacher02 的值
print(keys)
print(type(keys))
"""
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
li = ["Teacher01","Teacher02"]
keys = re.hmget("Teacher",li) #在 Teacher 对应的 hash 中获取 Teacher01 和 Teacher02 的值
print(keys)
print(type(keys))
"""
# 结果如下
['Mr.ZHENG', 'Mr.XIAO']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
num = re.hlen("Teacher") # 获取 Teacher 所对应 hash 中的键值对个数
print(num)
# 结果如下
3
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=0,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.hdel("Teacher","Teacher01","Teacher03") # 同时删除 Teacher01 和 Teacher02 键值对
dic = re.hgetall("Teacher") # 获取未被删除的键值对
print(dic)
# 结果如下
{'Teacher02': 'Mr.XIAO'}