Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding non-expiring keys in Redis

Tags:

redis

In my setup, the info command shows me the following:

[keys] => 1128 [expires] => 1125 

I'd like to find those 3 keys without an expiration date. I've already checked the docs to no avail. Any ideas?

like image 468
Duru Can Celasun Avatar asked Mar 22 '12 07:03

Duru Can Celasun


People also ask

Do Redis keys expire?

Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.

How do I get all Redis keys?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

What does Redis return if key doesnt exist?

Now technically you could just use the get command and if it returns an element then it means that the key exists and if it returns nil then it means that key did not exist.


2 Answers

Modified from a site that I can't find now.

redis-cli keys  "*" | while read LINE ; do TTL=`redis-cli ttl "$LINE"`; if [ $TTL -eq  -1 ]; then echo "$LINE"; fi; done; 

edit: Note, this is a blocking call.

like image 160
Waynn Lue Avatar answered Sep 22 '22 08:09

Waynn Lue


@Waynn Lue's answer runs but uses the Redis KEYS command which Redis warns about:

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases.

Redis documentation recommends using SCAN.

redis-cli --scan | while read LINE ; do TTL=`redis-cli ttl "$LINE"`; if [ $TTL -eq  -1 ]; then echo "$LINE"; fi; done; 

If you want to scan for a specific key pattern, use:

 redis-cli --scan --pattern "something*" 
like image 33
teastburn Avatar answered Sep 24 '22 08:09

teastburn