This tutorial will try to demystify how to work with Redis by showing how to work with keys.
NOTE: We do not intend this article to be a complete guide to Redis. It only plays a part in the entire concept of how Redis works.
Redis Insert Key
Let us start at the basics and discuss how you insert keys into a Redis database. Redis uses native commands using the Redis CLI. The Redis CLI is an interactive command-line environment for interacting with the Redis Cluster.
To open the Redis CLI, enter the command as:
The command above will connect to the Redis server using the default port (6379) and localhost. If Redis is hosted on a different host or port, you can specify them with -h and -p options, respectively.
Once connected, you should see a command prompt with the IP address and port of the Redis server:
To insert a key into Redis, we use the SET command. The command takes two arguments. The first argument acts as the key, and the second argument acts as the value for the specified key.
Let’s take a dataset containing state information. We can use the abbreviation as the key and the full name as the value.
Open the Redis CLI and run the command as shown below:
The above command will insert the key and its associated value to the Redis database. However, this method is not very efficient when inserting multiple values.
To solve this, we can use the Redis mass insertion.
Start by downloading the provided file containing a set of Redis commands and key-value pairs.
Once the file is downloaded, open the terminal and enter the command below to import the datasets.
The command above will export the data from the text file Redis.
Redis Retrieve Key
Use the GET command to get the value stored in a specific key. The command takes the key name as the argument.
The above should return the value associated with the specified key.
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.
As shown in the example above, the command should return all the keys in the database.
This guide shows you how to work with the Redis, from inserting keys to retrieving the keys matching a specific pattern.
Thank you for reading!!