Get all hashes exists in redis

ohadinho picture ohadinho · Dec 14, 2016 · Viewed 14.8k times · Source

I'm have hashes in redis cache like:

Hash         Key    Value
hashme:1    Hello   World
hashme:2    Here    Iam
myhash:1    Next    One

My goal is to get the Hashes as output in the CLI like:

hashme
myhash

If there's no such option, this is ok too:

 hashme:1
 hashme:2
 myhash:1

I didn't find any relevant command for it in Redis API.

Any suggestions ?

Answer

for_stack picture for_stack · Dec 14, 2016

You can use the SCAN command to get all keys from Redis. Then for each key, use the TYPE command to check if it's a hash.

UPDATE:

With Redis 6.0, the SCAN command supports TYPE subcommand, and you can use this subcommand to scan all keys of a specified type:

SCAN 0 TYPE hash

Also never use KEYS command in production environment!!! It's a dangerous command which might block Redis for a long time.