1、redis 基本操作命令
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.sadd("Animal","cat") # 给 Animal 集合中添加 1 个元素
print(re.smembers("Animal"))
re.sadd("Animal","dog","pig") # 给 Animal 集合中添加多个元素
print(re.smembers("Animal"))
# 结果如下
{'cat'}
{'pig', 'dog', 'cat'}
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.smembers("Animal")) # 获取 Animal 对应的值
# 结果如下
{'cat', 'dog', 'pig'}
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.srandmember("Animal",2))
# 结果如下
['dog', 'pig']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.smembers("Animal")) # 获取 Animal 对应的值
print(re.scard("Animal")) # 获取 Animal 对应的值的数量
# 结果如下
{'pig', 'cat', 'dog'}
3
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.sadd("set_name","aa","bb")
re.sadd("set_name1","bb","cc")
re.sadd("set_name2","bb","cc","dd")
print(re.sdiff("set_name","set_name1","set_name2")) # 输出存在 set_name 中但是不存在 set_name1 和 set_name2 中的元素
# 结果如下
{'aa'}
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.sadd("set_name","aa","bb")
re.sadd("set_name1","bb","cc")
re.sadd("set_name2","bb","cc","dd")
print(re.sinter("set_name","set_name1","set_name2")) # 输出 set_name,set_name1 和 set_name2 的交集
# 结果如下
{'bb'}
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.sadd("set_name","aa","bb")
re.sadd("set_name1","bb","cc")
re.sadd("set_name2","bb","cc","dd")
print(re.sunion("set_name","set_name1","set_name2")) # 获取 set_name,set_name1,set_name2 的并集
# 结果如下
{'bb', 'cc', 'aa', 'dd'}
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.sismember("Animal","cat")) # 判断 cat 存在于集合 Animal 中
# 结果如下
True
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.smove("set_name1","set_name","cc") # 将 set_name1 中的 cc 移动到 set_name 中
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.smembers("Animal"))
print(re.spop("Animal")) # 移除某个值(无序删除)
print(re.smembers("Animal"))
# 结果如下
{'dog', 'cat', 'pig'}
cat
{'dog', 'pig'}
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.smembers("Animal"))
re.srem("Animal","cat") # 指定删除 Animal 集合中的 cat 元素
print(re.smembers("Animal"))
# 结果如下
{'dog', 'cat', 'pig'}
{'dog', 'pig'}
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=1,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.smembers("Animal"))
re.srem("Animal","cat","pig") # 指定删除 Animal 集合中的 cat 和 pig 元素
print(re.smembers("Animal"))
# 结果如下
{'pig', 'cat', 'dog'}
{'dog'}
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
re.zadd("Animal",{"cat":1,"dog":2,"pig":3}) # 往有序集合 Animal 中加入元素值和其对应的分数
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zrange("Animal",0,2)) # 获取 Animal 有序集合中索引范围为 [0,2] 所对应的值
# 结果如下
['cat', 'dog', 'pig']
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zrange("Animal",0,2,desc=True,withscores=True)) # 获取 Animal 有序集合中索引范围为 [0,2] 所对应的值
# 结果如下
[('pig', 3.0), ('dog', 2.0), ('cat', 1.0)]
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zrevrange("Animal",0,2,withscores=True)) # 获取 Animal 有序集合中索引范围为 [0,2] 所对应的值
# 结果如下
[('pig', 3.0), ('dog', 2.0), ('cat', 1.0)]
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zrange("Animal",0,5,withscores=True))
print(int(re.zscore("Animal","pig"))) # 获取 Animal 对应有序集合中 pig 对应的分数
# 结果如下
[('cat', 1.0), ('dog', 2.0), ('pig', 3.0)]
3
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zcard("Animal"))
# 结果如下
3
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zcount("Animal",2,3)) # 获取有序集合 Animal 中分数在 [2,3] 之间的元素数量
# 结果如下
2
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zrange("Animal",0,5))
print(re.zrank("Animal","cat")) # 获取 cat 在 Animal 对应的有序集合中的排行位置,从 0 开始(按照分数从小到大排序)
print(re.zrevrank("Animal","cat")) # 获取 cat 在 Animal 对应的有序集合中的排行位置,从 0 开始(按照分数从大到小排序)
# 结果如下
['cat', 'dog', 'pig']
0
2
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zrange("Animal",0,5,withscores=True))
re.zrem("Animal","dog","cat") # 删除 Animal 对应的有序集合中值是 dog 和 cat 的成员
print(re.zrange("Animal",0,5,withscores=True))
# 结果如下
[('cat', 1.0), ('dog', 2.0), ('pig', 3.0)]
[('pig', 3.0)]
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zrange("Animal",0,5,withscores=True))
re.zremrangebyrank("Animal",0,2) # 删除 Animal 对应的有序集合中排行索引值 [0,2] 范围的元素
print(re.zrange("Animal",0,5,withscores=True))
# 结果如下
[('cat', 1.0), ('dog', 2.0), ('pig', 3.0)]
[]
import redis
pool = redis.ConnectionPool(host="192.168.0.221",port=6379,db=2,password="123456",decode_responses=True)
re = redis.Redis(connection_pool=pool)
print(re.zrange("Animal",0,5,withscores=True))
re.zremrangebyscore("Animal",0,2) # 删除 Animal 对应的有序集合中分数等于 0 或者 2 的元素
# re.zremrangebyscore("Animal",2,2) # 删除 Animal 对应的有序集合中分数等于 2 的元素
print(re.zrange("Animal",0,5,withscores=True))
# 结果如下
[('cat', 1.0), ('dog', 2.0), ('pig', 3.0)]
[('cat', 1.0)]
# [('cat', 1.0), ('pig', 3.0)]
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.delete("Student2","Fruits") # 删除 db0 中 Student2 和 Fruits 数据
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.delete("Student2","Fruits") # 删除 db0 中 Student2 和 Fruits 数据
print(re.exists("Student2")) # 判断在 db0 中 Student2 是否存在
print(re.exists("Student1"))
# 结果如下
0
1
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.rename("Student1","Student") # 将 Student1 重命名为 Student
print(re.get("Student"))
# 结果如下
zhaoliu
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.move("Student",4) # 将 Student 从 db0 移动到 db4 中
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.type("Student"))
print(re.type("Teacher"))
# 结果如下
string
hash