Introducing Dragonfly Cloud! Learn More

Retrieving Redis Keys Without TTL in PHP (Detailed Guide w/ Code Examples)

Use Case(s)

Retrieving all keys from a Redis instance without their Time-To-Live (TTL) value is a common use case when you just want to list the keys for management or debugging purposes without needing to know how long they will stay in the database before being automatically deleted.

Code Examples

Here’s an example using Predis, a flexible and feature-complete Redis client library for PHP:

require "vendor/autoload.php"; $predis = new Predis\Client(); $allKeys = $predis->keys('*'); foreach ($allKeys as $key) { echo $key . "\n"; }

In this code, keys('*') retrieves all keys matching any pattern, and then we iterate over each key to print it out. Note that this does not retrieve or display the TTL of each key; it just lists the keys themselves.

Best Practices

When using the KEYS command in Redis, be aware that it may impact performance when run against large databases, as it's an O(N) operation where N is the number of keys in the database. If possible, try to use more specific patterns with your KEYS command or consider using the SCAN command instead for larger databases.

Common Mistakes

One common mistake is not handling the possibility of no keys being returned by the keys function. It's recommended to always check whether any keys were returned before trying to use them.

FAQs

Q: Can I get keys with a specific pattern without TTL?

A: Yes, you can. Just replace the '*' in the keys('*') command with your specific pattern.

Was this content helpful?

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.