There is a very strange question,Unable to pass parameters to function。

Advertisements
import redis
_redis = redis.StrictRedis(host='localhost', port=6379, db=0)
print(_redis.expire('test', 3,nx=True))
Cannot pass parameter nx

report Error

redis.exceptions.ResponseError: wrong number of arguments for ‘expire’ command

this is function 


def expire(
        self,
        name: KeyT,
        time: ExpiryT,
        nx: bool = False,
        xx: bool = False,
        gt: bool = False,
        lt: bool = False,
    ) -> ResponseT:
        """
        Set an expire flag on key ``name`` for ``time`` seconds with given
        ``option``. ``time`` can be represented by an integer or a Python timedelta
        object.

        Valid options are:
            NX -> Set expiry only when the key has no expiry
            XX -> Set expiry only when the key has an existing expiry
            GT -> Set expiry only when the new expiry is greater than current one
            LT -> Set expiry only when the new expiry is less than current one

        For more information see https://redis.io/commands/expire
        """

i dont know why report this error plz help me ~

>Solution :

The nx, gx, gt, lt parameters on expire are only supported in Redis 7.0, so you’ll get this error if you’re using a lower version of the Redis server.

Officially mentioned in their standard doc:

History

Starting with Redis version 7.0.0: Added options: NX, XX, GT and LT.

Here it is in action using two Redis docker instances.

# docker run -p 6379:6379 -it redis:7
# docker run -p 6479:6379 -it redis:6
[ins] In [1]: import redis
[ins] In [3]: rd6 = redis.StrictRedis(port=6479)
[ins] In [4]: rd7 = redis.StrictRedis(port=6379)
[ins] In [5]: rd6.info()['redis_version']
Out[5]: '6.2.7'
[ins] In [6]: rd7.info()['redis_version']
Out[6]: '7.0.4'

[ins] In [7]: rd6.set("test", "data")
Out[7]: True
                        
[ins] In [9]: rd7.set("test", "data")
Out[9]: True

# The Redis 6.x instance doesn't recognize nx
[ins] In [10]: rd6.expire("test", 1000, nx=True)
---------------------------------------------------------------------------
ResponseError                             Traceback (most recent call last)

# etc.

ResponseError: wrong number of arguments for 'expire' command

# The Redis 7.x instance works fine
[ins] In [11]: rd7.expire("test", 1000, nx=True)
Out[11]: True

Leave a Reply Cancel reply