Redis command to get all available keys?

后端 未结 15 2663
攒了一身酷
攒了一身酷 2020-12-04 04:30

Is there a Redis command for fetching all keys in the database? I have seen some python-redis libraries fetching them. But was wondering if it is possible from redis-client.

15条回答
  •  旧巷少年郎
    2020-12-04 05:17

    SCAN doesn't require the client to load all the keys into memory like KEYS does. SCAN gives you an iterator you can use. I had a 1B records in my redis and I could never get enough memory to return all the keys at once.

    Here is a python snippet to get all keys from the store matching a pattern and delete them:

    import redis
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    for key in r.scan_iter("key_pattern*"):
        print key
    

提交回复
热议问题