Search code examples
redisnode-redisredis-cli

How to initiate, increment and set expiration in the same Redis command?


INCR mykey initiates to 0 and increments the value to 1. How would I set expiration time to the mykey in the same command like it can be done with SETEX ?


Solution

  • you can use pipeline or a lua script

    in fact, in redis documentation, there is an example of implementing rate limit that needs to increase and set ttl using multi

    https://redis.io/commands/INCR