添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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'}
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 中
# 结果如下
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'}
{'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'}
  • Zset 操作
  • 有序集合:在集合的基础上,为每元素排序,元素的排序需要根据另外一个值来进行比较
  • 对于有序集合,每一个元素有两个值,即:值和分数,分数专门用来做排序
  • 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 中加入元素值和其对应的分数
  • zrange( name,start,end,desc=False,withscores=False,score_cast_func=int)  按照索引范围 [start,end] 获取 name 对应的有序集合的元素
  • name:redis 的 name
  • start:有序集合索引起始位置
  • end:有序集合索引结束位置
  • desc:排序规则,默认按照分数从小到大排序
  • False:按分数从小到大排序
  • True:按分数从大到小排序
  • withscores:是否获取元素的分数,默认只获取元素的值
  • False:只获取元素值
  • True:获取元素值和分数
  • score_cast_func:对分数进行数据转换的函数
  • 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)]
  • zrevrange(name,start,end,withscores=False,score_cast_func=float)  按照索引范围 [start,end] 获取 name 对应的有序集合的元素
  • 用法同 zrange()
  • 默认按分数从大到小排序的
  • 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)]
    
    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"))
    # 结果如下
    
    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] 之间的元素数量
    # 结果如下
    
    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']
    
    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)]
  • delete(*names)  根据 name 删除 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)
    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"))
    # 结果如下
    
    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