Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all hashes exists in redis

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 ?

like image 315
ohadinho Avatar asked Dec 14 '16 07:12

ohadinho


People also ask

How can I get all my data from Redis?

Redis GET all 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.

How do I get Redis Hgetall?

Redis HGETALL command is used to get all the fields and values of the hash stored at the key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.

What is Hashkey in Redis?

A Redis hash is a data type that represents a mapping between a string field and a string value. Hashes can hold many field-value pairs and are designed to not take up much space, making them ideal for representing data objects.


4 Answers

keys * 

is work for me. you Can try it.

like image 87
Hui Tan Avatar answered Oct 18 '22 00:10

Hui Tan


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.

like image 34
for_stack Avatar answered Oct 18 '22 02:10

for_stack


The idea of redis (and others K/v stores) is for you to build an index. The database won't do it for you. It's a major difference with relational databases, which conributes to better performances.

So when your app creates a hash, put its key into a SET. When your app deletes a hash, remove its key from the SET. And then to get the list of hash IDs, just use SMEMBERS to get the content of the SET.

like image 3
Pascal Le Merrer Avatar answered Oct 18 '22 00:10

Pascal Le Merrer


connection.keys('*') this will bring all the keys irrespective of the data type as everything in redis is stored as key value format

like image 1
Gyanender Singhle Avatar answered Oct 18 '22 00:10

Gyanender Singhle