Installing Redis on Windows
Windows do not officially support Redis. However, you can set up and configure Windows Subsystem for Linux 2 to install Redis. An alternative is to use Docker, which I will discuss in this article, to run Redis inside a container.
Docker installation on a Windows computer is the initial step. From this page, you can get Docker Desktop. The installation procedure is quite easy to follow and uncomplicated. Enter the following command at a command prompt to download the Redis image from Docker Hub, which can be used to create and operate a container, now that Docker is installed on your computer.
Following the completion of this task, the third step entails starting a container using the Redis image that we downloaded previously.
In this case, press the “RUN” button.
Congratulations, the Redis server on your computer has now been successfully started.
Installing Redis-py
Redis-py is a Python package that will be used to connect to and use Redis in this tutorial. Run the following line in the command prompt to install it.
Now that everything is prepared, let’s get our hands dirty and start working on the programming portion.
Using Redis-py
We must establish a connection to the Redis server before carrying out any CRUD actions, which is simple to do in redis-py. Decode responses must be set to true in order to prevent receiving the response in byte format.
Let’s begin executing basic CRUD activities once we have established a connection to the Redis server.
The “set” function, which receives the key and the value as parameters, is used to set a key-value pair. Please keep in mind that the key must always either be a text data type or a set of bytes.
We utilize the “get” function, which receives the key for which we want the value to be returned, to retrieve the value for a given key.
The “mset” function, short for multiple-set, is used to set multiple key-value pairs. It takes multiple key-value pairs as inputs.
The “sadd” function is used to set a key-value pair where one of the values is of a specific data type. Unlike the list data type, the set data type only has unique elements. Let’s now store a collection of fruits without having them repeat.
Using the “smembers” function, we can retrieve all of the fruit values that we recently saved.
After learning how to store values for set data types in Redis, let’s move on to learning how to store values for list data types. The “lpush” function can be used to accomplish this. Using this function, let’s keep a list of all the programming languages.
We utilize the “lrange” function to traverse the list’s elements in order to obtain all of them. If you just want to return the first three entries of the list, simply replace the “-1” with a “2,” which indicates that the function is intended to return all elements. It is important to note that it is not “3” because it will traverse till the index is three and return a total of four entries.
Let’s attempt to save a nested object in Redis now. The “hset” method can be used to store nested items, although only one level of nesting is supported.
Serialization techniques like json or pickle can be used to store deeply nested objects with various data types. To further comprehend, let’s observe this in action.
We can retrieve the data from the storage right away using the “get” function, and then we can reverse the stringification of JSON.
Since it is an in-memory data store, it is crucial that outdated key-value pairs are destroyed or rather allowed to expire in order to provide room for fresh data to be stored. Redis includes a key expiry option that can be used for this. Let’s try storing a key-value pair with a time limit now. The “setex” function can be used to specify when a key-value pair expires. Within seconds, it accepts the TTL. The “psetex” function can be used to set the TTL in milliseconds.
Using the “ttl” function, which returns the time left in seconds, we can find out how long a key has until it expires. The “pttl” function will also return the same result but in milliseconds. The returned value will be negative if the key has expired.
The “exists” method, which returns 1 if the key is available and 0 if it has expired, can be used to determine whether a key has expired or not.
We can use Redis’ “delete” function to remove a key-value pair. You can enter the key you want to remove.
You can also view and read our other article related to data science, big data, and their implementation here: