You may need to get all keys from a Redis store that do not have an expiration set (TTL). This can be useful for auditing, debugging or implementing specific business logic.
In Node.js, using the
node-redis package, there isn't a direct way to fetch keys without TTL. Instead, you'll need to fetch all keys and then filter out keys with a TTL. Here's how to do it:
This script gets all keys ('*') and checks each key's TTL. Keys without an expiration have a TTL of -1.
Avoid running the script frequently on production systems as the 'keys' command can be resource-intensive, especially if you have a large number of keys.
One common mistake is not checking for errors when calling
client.ttl. Always include error handling to ensure you can catch and handle any potential issues.
Q: Can I get keys without TTL directly? A: No Redis does not provide a direct way to fetch keys without TTL. You need to fetch all keys first and then check each key's TTL.
Q: What does a TTL of -1 mean? A: In Redis, a TTL (Time To Live) of -1 means the key does not have an expiration time set.