Redis: how to set the expiration date for a list update

I want to save a set of indefinite length in redis, and the whole set should expire in a few minutes.
So I do:
RPUSH 'a' 'b'
EXPIRE 'a' 120
RPUSH 'a' 'c'
EXPIRE 'a' 120

but in this case I only have 'c' in 'a', because the first RPUSH after EXPIRE processes the old value.
Install EXPIRE first, we have TTL β†’ - 1 after the first RPUSH.

So my question is, is there a way to do what I want?

+4
source share
1 answer

the first RPUSH after EXPIRE processes the old value

This is not behavior or Redis. Are you sure that LIST a not just expired when you add c to it? I did a quick check with redis-cli and confirmed that Redis behaves exactly the way you want it to use your approach:

 RPUSH test foo EXPIRE test 120 RPUSH test bar EXPIRE test 120 LRANGE test 0 -1 # 1) "foo" # 2) "bar" 

The EXPIRE call correctly resets the expiration, so it increases by 120 seconds. Any changes to the list do not affect the validity period or existing values.

+2
source

All Articles