0
点赞
收藏
分享

微信扫一扫

Python脚本之操作Redis【二】

年夜雪 2022-02-09 阅读 51
redispython

本文为博主原创,未经授权,严禁转载及使用。
本文链接:https://blog.csdn.net/zyooooxie/article/details/95352530

之前分享过一篇 操作Redis【一】 ,主要写的是 String、Hash类型的key;
这一次来分享下 List、Set的;

【实际这篇博客推迟发布N个月】

个人博客:https://blog.csdn.net/zyooooxie

【以下所有内容仅为个人项目经历,如有不同,纯属正常】

Redis命令【二】

先前说过一个学习命令的网站,再加个 https://redis.io/commands

  1. List:

LINDEX KEY_NAME index 【index 正0开始;反 -1开始】
LLEN KEY_NAME
LRANGE KEY_NAME start stop

LSET KEY_NAME index new_VALUE
LINSERT KEY_NAME BEFORE|AFTER pivot new_VALUE 【插入、新增】

LPUSH KEY_NAME VALUE1 VALUE2 【插入到 列表 的表头】
RPUSH KEY_NAME VALUE1 VALUE2 【插入到列表 的表尾】

LPOP KEY_NAME 【移除并返回 列表的头元素】
RPOP KEY_NAME 【移除并返回 列表 的尾元素】
LREM KEY_NAME count VALUE1

  1. Set:

SCARD KEY_NAME
SMEMBERS KEY_NAME
SISMEMBER KEY_NAME member

SADD KEY_NAME member

SREM KEY_NAME member

代码

"""
@blog: https://blog.csdn.net/zyooooxie
"""

def test_csdn4():
    r = redis_connect(gl_host, gl_port, gl_pwd)

    print(r.ttl(gl_real_list))
    print(r.ttl(gl_real_set))
    print(r.ttl(gl_real_string))
    print(r.ttl(gl_real_hash))
    print()

    test_list = 'CROSS_2022:zyooooxie:list'

    # LINDEX命令 - 取值(根据索引号取值)
    # LINDEX key index  -- 返回列表 key 中,下标为 index 的元素
    # 下标(index)参数 start 和 stop 都以 0 为底,也就是说,以 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。
    # 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
    # 如果 key 不是列表类型,返回一个错误。

    # 返回值: 列表中下标为 index 的元素。
    # 如果 index 参数的值不在列表的区间范围内(out of range),返回 nil 。

    print(r.lindex(test_list, 0))                               # key不存在,返回的None
    print(r.lindex(gl_real_list, 1111111))                      # index超了,返回的None

    print(r.llen(gl_real_list))             # 10

    # lindex(): Return the item from list ``name`` at position ``index``
    print(r.lindex(gl_real_list, 0))
    print(r.lindex(gl_real_list, 3))
    print(r.lindex(gl_real_list, 300))       # index超了

    # print(r.lindex(gl_real_set, 0))                         # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lindex(gl_real_string, 0))                          # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lindex(gl_real_hash, 0))                            # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    # Negative indexes are supported and will return an item at the end of the list
    print(r.lindex(gl_real_list, -1))
    print(r.lindex(gl_real_list, -5))

    print()

    # LLEN 命令 -  列表长度
    # LLEN key- 返回列表 key 的长度。
    # 如果 key 不存在,则 key 被解释为一个空列表,返回 0 .
    # 如果 key 不是列表类型,返回一个错误。

    # 返回值: 列表 key 的长度。

    # llen(): Return the length of the list ``name``
    print(r.llen(test_list))                        # key不存在,返回的0
    print(r.llen(gl_real_list), 'llen()取值')

    # print(r.llen(gl_real_set))                      # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.llen(gl_real_string))                       # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.llen(gl_real_hash))                         # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    print()

    # LRANGE命令 - 切片取出值,范围是索引号0到-1(最后一个元素)
    # LRANGE key start stop  -- 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定。
    # 下标(index)参数 start 和 stop 都以 0 为底,也就是说,以 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。
    # 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。

    # stop下标也在 LRANGE 命令的取值范围之内(闭区间);       和Python的 range() 函数 不同!

    # 超出范围的下标值不会引起错误。
    # 如果 start 下标比列表的最大下标 end ( LLEN list 减去 1 )还要大,那么 LRANGE 返回一个空列表。
    # 如果 stop 下标比 end 下标还要大,Redis将 stop 的值设置为 end 。

    # 返回值: 一个列表,包含指定区间内的元素。

    # lrange(): Return a slice of the list ``name`` between position ``start`` and ``end``
    # ``start`` and ``end`` can be negative numbers
    print(r.lrange(test_list, 0, -1))               # key不存在,返回[]

    print(r.lrange(gl_real_list, 0, -1))
    print(r.lrange(gl_real_list, 0, 2))

    # print(r.lrange(gl_real_set, 0, 2))              # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lrange(gl_real_string, 0, 2))               # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lrange(gl_real_hash, 0, 2))                 # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    print(r.lrange(gl_real_list, 9, -1))             # 长度为10,返回[最后一个元素]
    print(r.lrange(gl_real_list, 9, 8))             # 返回[]

    print(r.lrange(gl_real_list, 10, 12))             # 长度为10,返回[]
    print(r.lrange(gl_real_list, 8, 12))             # 长度为10,返回[最后2个元素]

    print()

    # RPUSH 命令 - 增加(从右边增加),没有就新建
    # RPUSH key value [value ...]  将一个或多个值 value 插入到列表 key 的表尾(最右边)。
    # 如果有多个 value 值,那么各个 value 值按从左到右的顺序依次插入到表尾。

    # 如果 key 不存在,一个空列表会被创建并执行 RPUSH 操作。
    # 当 key 存在但不是列表类型时,返回一个错误。

    # 返回值: 执行 RPUSH 操作后,表的长度。
    r.delete(test_list)

    # rpush(): Push ``values`` onto the tail of the list ``name``
    print(r.rpush(test_list, '1f', '2s'))            # 新插入元素在右侧,如果list不存在则新建
    print(r.exists(test_list))
    print(r.lrange(test_list, 0, -1))

    print(r.rpush(test_list, '1f22', '2s22'))                   # 返回 LLEN()
    print(r.llen(test_list))
    print(r.lrange(test_list, 0, -1))

    print(r.rpush(test_list, '1f22', '2s22'))                   # 重复添加
    print(r.lrange(test_list, 0, -1))

    print(r.rpush(test_list, 'zyooooxie'))                   # 添加一个
    print(r.lrange(test_list, 0, -1))

    # print(r.rpush(gl_real_set, 'zyooooxie'))                   # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.rpush(gl_real_string, 'zyooooxie'))                   # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.rpush(gl_real_hash, 'zyooooxie'))                   # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    print()

    print('LPUSH命令 -- 将一个或多个值 value 插入到列表 key 的表头')
    print(r.lpush(test_list, 'zyooooxie'))
    print(r.lrange(test_list, 0, -1))

    print(r.lpush(test_list, 'zyooooxie', '123'))
    print(r.lrange(test_list, 0, -1))

    print()

    print(r.ttl(test_list))
    print(r.expire(test_list, 666))
    print(r.ttl(test_list))

    print()

    # LINSERT命令 - 新增(固定索引号位置 插入元素)
    # LINSERT key BEFORE|AFTER pivot value  --  将值 value 插入到列表 key 当中,位于值 pivot 之前或之后。
    # 当 pivot 不存在于列表 key 时,不执行任何操作。 当 key 不存在时, key 被视为空列表,不执行任何操作。
    # 如果 key 不是列表类型,返回一个错误。

    # 返回值:
    # 如果命令执行成功,返回插入操作完成之后,列表的长度。
    # 如果没有找到 pivot ,返回 -1 。
    # 如果 key 不存在或为空列表,返回 0 。

    r.delete(test_list)

    # linsert(): Insert ``value`` in list ``name`` either immediately before or after  [``where``] ``refvalue``
    # Returns the new length of the list on success or -1 if ``refvalue`` is not in the list.
    print(r.linsert(gl_real_list, 'BEFORE', 'zyooooxie', 'https://blog.csdn.net/zyooooxie'))            # -1 非空列表插入时,传一个不存在的 pivot
    print(r.linsert(gl_real_list, 'AFTER', 'zyooooxie', 'https://blog.csdn.net/zyooooxie'))             # -1

    print(r.exists(test_list))                                          # 0 不存在
    print(r.linsert(test_list, 'before', 'zyooooxie', 'https://blog.csdn.net/zyooooxie'))               # 0

    # print(r.linsert(gl_real_set, 'before', 'zyooooxie', 'https://blog.csdn.net/zyooooxie'))             # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.linsert(gl_real_string, 'before', 'zyooooxie', 'https://blog.csdn.net/zyooooxie'))              # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.linsert(gl_real_set, 'before', 'zyooooxie', 'https://blog.csdn.net/zyooooxie'))             # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    r.rpush(test_list, 'new0')
    print(r.linsert(test_list, 'BEFORE', 'new0', 'insert的1'))           # where: BEFORE(前)或AFTER(后)
    print(r.llen(test_list))
    print(r.lrange(test_list, 0, -1))

    print(r.linsert(test_list, 'AFTER', 'new0', 'insert的3'))
    print(r.llen(test_list))
    print(r.lrange(test_list, 0, -1))

    print(r.linsert(test_list, 'before', 'new0', 'insert的2'))
    print(r.lrange(test_list, 0, -1))

    print(r.linsert(test_list, 'after', 'new0', 'insert的4'))
    print(r.lrange(test_list, 0, -1))

    print()

    # LPOP 命令 - 删除并返回第一个元素
    # LPOP key- 移除并返回列表 key 的头元素。

    # 返回值: 列表的头元素。 当 key 不存在时,返回 nil 。

    # lpop(): Remove and return the first item of the list ``name``
    print(r.lpop(test_list), 'lpop()执行')
    print(r.lrange(test_list, 0, -1))

    print(r.lpop(test_list), 'lpop()执行 第2次')
    print(r.lrange(test_list, 0, -1))

    # print(r.lpop(gl_real_set))                      # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lpop(gl_real_string))                   # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lpop(gl_real_hash))                     # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    print('https://redis.io/commands/lpop')
    # Redis version >= 6.2.0: Added the `count` argument.

    # # lpop(): When provided with the optional ``count`` argument, the reply will consist of up to count elements, depending on the list's length.
    # print(r.lpop(test_list, count=2))

    print()

    print('RPOP -- 移除并返回列表 key 的尾元素')
    print(r.rpop(test_list), 'rpop()执行')
    print(r.lrange(test_list, 0, -1))

    print()

    # LREM 命令 - 删除(指定值进行删除)
    # LREM key count value  -- 根据参数 count 的值,移除列表中与参数 value 相等的元素。

    # count 的值可以是以下几种:
    # count > 0 : 从表头开始向表尾搜索,移除与 value 相等的元素,数量为 count 。
    # count < 0 : 从表尾开始向表头搜索,移除与 value 相等的元素,数量为 count 的绝对值。
    # count = 0 : 移除表中所有与 value 相等的值。

    # 返回值: 被移除元素的数量。
    # 因为不存在的 key 被视作空表(empty list),所以当 key 不存在时, LREM 命令总是返回 0 。

    # print(r.lrem(gl_real_set, count=0, value='zyooooxie'))              # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lrem(gl_real_string, count=0, value='zyooooxie'))           # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lrem(gl_real_hash, count=0, value='zyooooxie'))             # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    print(r.lrem(gl_real_list, count=0, value='zyooooxie'))         # 0 是value不存在
    print(r.lrem(gl_no_exist, count=0, value='zyooooxie'))         # 0 是key不存在

    print(r.lpush(test_list, *['zyooooxie'] * 3))
    print(r.rpush(test_list, *['zyooooxie'] * 4))
    print(r.lrange(test_list, 0, -1))

    # lrem(): Remove the first ``count`` occurrences of elements equal to ``value`` from the list stored at ``name``.
    # The count argument influences the operation in the following ways:
    #     count > 0: Remove elements equal to value moving from head to tail.
    #     count < 0: Remove elements equal to value moving from tail to head.
    #     count = 0: Remove all elements equal to value.

    # 移除从表头到表尾,最先发现的两个
    print(r.lrem(test_list, count=2, value='zyooooxie'))                # 两个元素被移除

    # 移除从表尾到表头,3个
    print(r.lrem(test_list, count=-3, value='zyooooxie'))
    print(r.lrange(test_list, 0, -1))

    print(r.linsert(test_list, 'after', 'new0', 'zyooooxie'))
    print(r.lrange(test_list, 0, -1))

    # 移除表中所有的
    print(r.lrem(test_list, count=0, value='zyooooxie'))
    print(r.lrange(test_list, 0, -1))

    print()

    # LSET命令 - 修改(指定索引号进行修改)  - 【重新赋值,不新增】
    # LSET key index value   -- 将列表 key 下标为 index 的元素的值设置为 value。
    # 当 index 参数超出范围,或对一个空列表( key 不存在)进行 LSET 时,返回一个错误。

    # 返回值:  操作成功返回 ok ,否则返回错误信息。

    # r.lset(test_list, 2222, 'new-4')                   # 超出范围 redis.exceptions.ResponseError: index out of range

    print(r.exists(gl_no_exist))
    # print(r.lset(gl_no_exist, 0, 'zyooooxie'))         # redis.exceptions.ResponseError: no such key

    # print(r.lset(gl_real_string, 1, 'zyooooxie'))                   # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lset(gl_real_hash, 1, 'zyooooxie'))                     # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.lset(gl_real_set, 1, 'zyooooxie'))                      # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    print(r.lrange(test_list, 0, -1))

    # lset(): Set ``position`` of list ``name`` to ``value``
    print(r.lset(test_list, 1, 'new-1新值'))                   # True
    print(r.lset(test_list, 0, 'new改0'), 'lset() 第1次')
    print(r.lrange(test_list, 0, -1))

    print(r.lset(test_list, 0, 'new-0-新值'), 'lset() 第二次')
    print(r.lrange(test_list, 0, -1))

    r.expire(test_list, 666)

    r.close()

"""
@blog: https://blog.csdn.net/zyooooxie
"""

def test_csdn5():
    r = redis_connect(gl_host, gl_port, gl_pwd)

    print(r.ttl(gl_real_list))
    print(r.ttl(gl_real_set))
    print(r.ttl(gl_real_string))
    print(r.ttl(gl_real_hash))
    print()

    test_set = 'CROSS_2022:zyooooxie:set'

    # SADD 命令 - 新增
    # SADD key member [member ...]  --  将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。

    # 假如 key 不存在,则创建一个只包含 member 元素作成员的集合。
    # 当 key 不是集合类型时,返回一个错误。

    # 返回值: 被添加到集合中的新元素的数量,不包括被忽略的元素。

    print(r.exists(test_set))

    # print(r.sadd(gl_real_string, 'zyooooxie'))              # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.sadd(gl_real_hash, 'zyooooxie'))                # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.sadd(gl_real_list, 'zyooooxie'))                # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    # sadd(): Add ``value(s)`` to set ``name``
    r.delete(test_set)
    print(r.sadd(test_set, 'zyooooxie'))

    # 添加重复元素 会被忽略
    print(r.sadd(test_set, 'zyooooxie', 'zyooooxie', 'https://blog.csdn.net/zyooooxie', 'zyooooxie', 'csdn'))
    print(r.sadd(test_set, 'csdn', 'https://blog.csdn.net/zyooooxie', 'zyooooxie'))

    print()

    # SCARD命令 - 获取元素个数 类似于len
    # SCARD key -- 返回集合 key 的基数(集合中元素的数量)。

    # 返回值: 集合的基数。 当 key 不存在时,返回 0 。

    # scard(): Return the number of elements in set ``name``
    print(r.scard(test_set), 'scard()')

    r.sadd(test_set, 'new')
    print(r.scard(test_set), 'scard() 第二次')

    print(r.scard(gl_no_exist), 'key 不存在时')
    # print(r.scard(gl_real_string))              # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.scard(gl_real_hash))                # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.scard(gl_real_list))                # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    print()

    # SMEMBERS 命令 - 获取集合中所有的成员
    # SMEMBERS key  -- 返回集合 key 中的所有成员。
    # 不存在的 key 被视为空集合。

    # 返回值: 集合中的所有成员。

    # smembers(): Return all members of the set ``name``
    print(r.smembers(gl_no_exist), 'key 不存在')                  # set() 空集合
    print(r.smembers(test_set))

    print(r.sadd(test_set, *['new2222'] * 5))
    print(r.smembers(test_set))

    print(r.sadd(test_set, *('new2222' * 5)))
    print(r.smembers(test_set))

    print()

    print(r.ttl(test_set))
    print(r.expire(test_set, 666))
    print(r.ttl(test_set))

    print()

    # SISMEMBER 命令- 判断是否是集合的成员 类似in
    # SISMEMBER key member  --  判断 member 元素是否集合 key 的成员。

    # 返回值:
    # 如果 member 元素是集合的成员,返回 1 。
    # 如果 member 元素不是集合的成员,或 key 不存在,返回 0 。

    # sismember(): Return a boolean indicating if ``value`` is a member of set ``name``
    # TODO 结果为True和False
    print(r.sismember(gl_no_exist, 'zyooooxie'), 'key 不存在')                 # False

    # print(r.sismember(gl_real_hash, 'zyooooxie'))                       # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.sismember(gl_real_string, 'zyooooxie'))                     # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.sismember(gl_real_list, 'zyooooxie'))                       # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    print(r.sismember(test_set, 'zyooooxie'))
    print(r.sismember(test_set, 'xie'))

    print()

    # SREM 命令 - 指定值删除
    # SREM key member [member ...]   --  移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略。
    # 当 key 不是集合类型,返回一个错误。

    # 返回值:   被成功移除的元素的数量,不包括被忽略的元素。

    # srem(): Remove ``values`` from set ``name``
    print(r.srem(gl_no_exist, 'zyooooxie'), 'key 不存在')

    # print(r.srem(gl_real_hash, 'zyooooxie'))            # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.srem(gl_real_string, 'zyooooxie'))          # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
    # print(r.srem(gl_real_list, 'zyooooxie'))            # redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

    print(r.srem(test_set, 'zyooooxie'))
    print(r.smembers(test_set))

    print(r.srem(test_set, 'zyooooxie', 'csdn', 'zyooooxie', '不存在的', 'new'))
    print(r.smembers(test_set))

    print(r.srem(test_set, '不存在的'))
    print(r.smembers(test_set))

    r.close()

本文链接:https://blog.csdn.net/zyooooxie/article/details/95352530

交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie

举报

相关推荐

0 条评论