Finding non-expiring keys in Redis

后端 未结 4 1776
盖世英雄少女心
盖世英雄少女心 2020-12-24 01:42

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

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

I\'d like to find those 3 keys wi

相关标签:
4条回答
  • 2020-12-24 02:07

    @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*"
    
    0 讨论(0)
  • 2020-12-24 02:22
    #!/usr/bin/env python
    
    import argparse
    import redis
    
    p = argparse.ArgumentParser()
    p.add_argument("-i", '--host', type=str, default="127.0.0.1", help="redis host", required=False)
    p.add_argument("-p", '--port', type=int, default=6379, help="redis port", required=False)
    p.add_argument("-n", '--db', type=int, default=0, help="redis database", required=False)
    
    args = p.parse_args()
    
    r = redis.Redis(host=args.host, port=args.port, db=args.db)
    
    try:
        keys = r.keys()
    
        for key in keys:
            if r.ttl(key) < 0:
                print(key)
    except KeyboardInterrupt:
        pass
    
    0 讨论(0)
  • 2020-12-24 02:30

    In case somebody is getting bad arguments or wrong number of arguments error, put double quotes around $LINE.

    So,it would be

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

    This happens when there are spaces in the key.

    0 讨论(0)
  • 2020-12-24 02:33

    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.

    0 讨论(0)
提交回复
热议问题