redis python client lrem函数的使用

博客探讨了在使用redis-py客户端时,对lrem函数的理解和应用。作者最初按自己的理解错误地调用了该函数,但通过尝试和阅读源代码,发现了正确的参数顺序。文章包含了测试代码和对Redis与StrictRedis中lrem方法的区分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请先看最后的纠正说明,谢谢

以前从来没有用过redis,现在有个任务需要使用redis,要求是能够根据index来删除key所对应的value list里面的值


def lrem(self, name, count, value):
        """
        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.
        """
        return self.execute_command('LREM', name, count, value)


API 来自:redis-py client source code

redis-py API就是上面这样解释的。

然后我就认为第一个就是key,第二个参数就是说明里面的可以>0 , < 0 或 =0,最后一个是要删除的value值,


所有我写的code如下所示:

ret_code = redis_client.lrem(queue_name, 0, item)

打印ret_code的值始终为0,表示删除失败。我就很纳闷。通过redis-cli可以成功删除的

joey@ubuntu:~/workspace/py$ redis-cli
redis 127.0.0.1:6379> select 2
OK
redis 127.0.0.1:6379[2]> lrange list1 0 -1
1) "two"
2) "four"
3) "siz"
4) "seven"
5) "eight"
6) "nine"
7) "ten"
redis 127.0.0.1:6379[2]> lrem list 0 four
(integer) 0
redis 127.0.0.1:6379[2]> lrem list1 0 four
(integer) 1
redis 127.0.0.1:6379[2]> lrange list1 0 -1
1) "two"
2) "siz"
3) "seven"
4) "eight"
5) "nine"
6) "ten"
redis 127.0.0.1:6379[2]> 

就是程序始终删除失败,网上搜索也搜不到相关主题内容。

后来我就活马当成死吗医,就把参数顺序随便那么一改,奇迹出现了。删除成功了,我回过头继续读源码解释,我现在在想,是我理解错了么??难道我英语真的差劲吗???


以下是我的测试程序:


#!/usr/bin/env python
#-*- coding:utf-8 -*-

import redis
import os
import sys


host = '127.0.0.1'
port = 6379
db = 2

queue_name = 'list1'

val = 'five'

def test():
    redis_client = redis.Redis(host, port, db)
    
    index = 0
    job = None
    for item in redis_client.lrange(queue_name, 0, -1):
        if item.__eq__(val):
            job = redis_client.lindex(queue_name, index)
            result = redis_client.lrem(queue_name, job, 0)
            print 'return code: ', result
            break
        index = index + 1
    print '========================='
    for item in redis_client.lrange(queue_name, 0, -1):
        print item

if __name__=='__main__':
    test()
    

========================================================

纠正:redis中有两个class,一个是Redis,一个是StrictRedis,

两个里面各有lrem方法:

Redis中的lrem方法:

lrem(name, value, num=0)
Remove the first num occurrences of elements equal to value from the list stored at name.

The num argument influences the operation in the following ways:
num > 0: Remove elements equal to value moving from head to tail. num < 0: Remove elements equal to value moving from tail to head. num = 0: Remove all elements equal to value.

StrictRedis中的lrem方法:

lrem(name, count, value)
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.

所以说之前是方法找错了。哎。。。。。。。。。。。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值