Reputation: 10439
I have some keys which have expiration in redis. I check them with EXISTS
in my application and I decide based on its output.
When they reach their expiration time, the TTL
command, returns -1
, but they are not removed from redis and EXISTS
command still returns 1
for them; which my app assumes that they are not yet expired.
However, since I realized that these keys will be deleted once someone tries to access them using SCAN
or similar commands, I tried to do the same. I used SCAN
on my key, but it didn't delete my key.
Is there any command to check if a key is not expired? A command that returns 1
only if both following conditions are true:
Upvotes: 2
Views: 1860
Reputation: 9596
In Redis 2.6 or older the TTL command returns -1
if the key does not exist or if the key exists but has no associated expiration.
Starting with Redis 2.8 the return value in case of an error changed:
-2
if the key does not exist.-1
if the key exists but has no associated expiration.So in your case, your keys exist but they don't have an associated expiration. Actually, you are accessing the keys with your EXISTS
commands executed on your keys, but they don't have an expiration - so they are not expired.
So you need to set an expiration for those keys, then they will be expired actively(why you accessed them) or passively (with redis's expiration algorithm running behind the scenes)
Upvotes: 1