Redis is a popular open-source,key-value type storage. Basically, redis saves the data in keys and value format. In this blog, we will learn how to get all keys in redis. so let’s get started.
Redis get the value of a key
If you are a beginner, then you can follow the below tutorial to install redis on your system based on the operating system.
Let’s insert the below 5 keys into the redis instance.
set my_key1 my_value1
set my_key2 my_value2
set my_key3 my_value3
set my_key4 my_value4
set my_key5 my_value5
To get a value of a single key user needs to use the GET command followed by the key_name.
get my_key1
The above query will return the value of my_key1
Get all keys in redis
Similarly to get all the keys from a redis instance user needs to use the pound Asterisk(*) sign with KEYS.
KEYS *
The above query returns all keys in the redis instance.
Get all keys in redis with the prefix
So far we have seen how to get all keys in redis.In this session, we will understand how to get keys using regex. Let’s insert two more keys into our redis instance.
set key1 value1
set key2 value2
Check all the keys in the redis instance
keys *
Now let’s assume you wish to get the keys which start from my_. You can use the below command to achieve the same.
KEYS my_*
Redis get all keys python
In this session, we will understand how to retrieve all the keys in redis using the python client. If you are a beginner you can follow my tutorial on redis python to install a python client to interact with redis.
Now users can refer to the below code to get all the keys in redis using python.
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.keys()
Conclusion
I hope you have liked this small tutorial on redis get all keys. Please do let me know if you are facing any issues while following along.