Zeinab Abbasimazar
Zeinab Abbasimazar

Reputation: 10439

scan does not delete expired keys in redis

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:

  1. The key exists in redis
  2. The expiration of the is positive

Upvotes: 2

Views: 1860

Answers (1)

Ersoy
Ersoy

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:

  • The command returns -2 if the key does not exist.
  • The command returns -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

Related Questions