Redis OM is a widely used in-memory database deployed as a cache or database and message broker. It is well-suited for high-performance, real-time applications that need low-latency data access. Redis supports several data types, including strings, lists, sets, and hyperloglogs. Redis-py is one of the most used Redis Clients for python to access the Redis Database. It allows developers to interact with different types of data stored in Redis, supports pipelining, connection pooling, and thread safety, and provides support for Pub/Sub, Lua scripting, and transaction support. Redis-py maps closely to the Redis Command-Line.
This article was published as a part of the Data Science Blogathon.
If there already exists a Redis client already, then why need for another client? To understand this, let’s add a student’s details to a student hash in the Redis database. In Redis CLI, we do this in the following way.
127.0.0.1:6379> HMSET student Name Rohan Class 11
The above line will add Rohan’s name and class details to the student hash. Now let us look at the Redis-py implementation for the same.
import redis client = redis.Redis(host='127.0.0.1',port=5379,decode_responses=True) rohan = { "Name" : "Rohan" , "Class" : 11 } client.hmset( "student" , mapping = rohan )
So it is seen that the Redis-py is a low-level client whose functions are very much similar to the Redis commands. Using this client, one has to remember all the Redis commands out there so to create and store data in Redis. This is where the Redis OM comes into play.
Redis OM is a high-level, object-oriented API for working with data in Redis. The Redis OM is a newly built Redis client that is a high-level abstraction based upon the Redis by itself, making it a more opinionated and higher-level API for data management and manipulation.
The main features of Redis OM include declarative object mapping, data validation, and serialization. All Redis OM models are Pydantic models, allowing you to use Pydantic’s robust and extensible data validation features.
Before installing Redis OM make sure you are running on a Python version 3.7 or above and make sure you have a Redis server up and running. To get Redis running on your system, you can either directly download it from the official Redis site or download a Redis docker file from the DockerHub.
Run the following command in the shell or CMD to download and install the Redis OM module.
pip install redis-om
It will install the Redis-om module and the supporting packages.
Redis Object Model (Redis OM) is a powerful tool for managing data in Redis. It gives developers access to a high-level, object-oriented API for working with Redis data and makes it simple for them to carry out challenging data operations. In this section, we will explore how to get started with Redis-om and how to define data models using Redis-om
Let’s look at some sample codes below.
from redis_om import HashModel from typing import Optional from pydantic import EmailStr class Student(HashModel): name: str age: int email: EmailStr about: Optional[str]
We created a Student model in the above code that extends the HashModel. Here the HashModel class is one of the Redis OM classes used when we want to store a Redis hash. A hashmap stores key-value pairs, where the key is a unique string.
This class allows you to define the fields of the hashmap using Pydantic models, which means you can add the data types, constraints, and default values for each field. Thus, we provided the necessary fields like name, age, email, and about that the hash takes in, along with their respective type annotations.
When a model inherits HashModel, it is inheriting a Redis OM model and even a Pydantic model. So, it means that all the data validations that can be applied in a Pydantic model can be applied here. That is why we can add EmailStr, which is available in the Pydantic library.
Redis does not support nested hash or nested sets or lists. So HashModel doesn’t take another hash.
Now let us assign a variable to the Student model.
karan = Student( name = "Karan", age = 28, email = "[email protected]", about = "I'm a Tech Geek" )
The above code runs, and the fields are provided and match their respective types. Now, what happens if you do not provide the about the field? Nothing. As the field is set to optional.
And what if we do not provide the email? The code then throws out a Validation Error, stating that one of the fields is missing. Also, when you provide an invalid email address, a Validation Error is thrown again, stating ‘value is not a valid email address‘.
This makes the code robust, thus saving a lot of time with respect to missing data and data validation.
Accessing field values from the model is fairly easy compared to the way you access through the Redis-py client, where you have to use the client.hget() command to access a specific field value from a Redis hash. In Redis-om, we access the values in the following way.
print(karan.email) [email protected]
In the above code, we can see how simple it is to access the field values from the model, similar to how we access the attributes from a class, that is, in an object-oriented fashion.
Redis is an in-memorydatabase, and one of its key features is its ability to handle large amounts of data quickly and efficiently. In this section, we will discuss how to manage data in Redis using the Redis OM and PKs, short for primary keys. Specifically, we will cover how to save, retrieve, and use PKs to access and manipulate data in Redis efficiently. By the end of this article, you will be able to understand how to use Redis OM to save data in Redis and how to access it with the help of PKs.
Redis OM allows you to manipulate the data in an object-oriented way. For example, to save a data model to Redis, we call the save() function to that data model. It can be done as follows:
karan.save()
Now the data stored in the karan variable is available on Redis. But how do we access it in the code? This can be done using the primary key.
Redis OM automatically generates a primary key without interacting with the Redis server. In Redis OM, a PK is a unique identifier used to identify and access a specific piece of data. It’s used to access and manipulate data in Redis efficiently. It is generated such that it is globally unique. To get the PK of the data, the pk function has to be called on that model.
The get() function on the model class can be used to retrieve a model’s data if you know its PK. The below code explains it all.
print(karan.pk) # Output -> 01GQ2XE5MH935FS5H2WTGEY9DG
print(Student.get(karan.pk)) # Output -> pk='01GQ2XE5MH935FS5H2WTGEY9DG' name='Karan' age=28 email='[email protected]' about="I'm a Tech Geek"
We can see that by calling in Student.get() by giving it the PK of the model; we can retrieve the data.
After saving the data to the Redis server, we know that the data now exists in Redis. So how do we know the name of the key for that specific Redis hash? Key () can be used to retrieve the hash’s key.
print(karan.key()) # Output -> :__main__.Student:01GQ2YYFG16Z166EJ4YAKXT4W
“:__main__.Student:01GQ2YYFG16Z166EJ4YAKXT4W” is the key where the hash is stored. You can verify this by running the HGETALL function on the key in Redis CLI.
127.0.0.1:6379> HGETALL :__main__.Student:01GQ2YYFG16Z166EJ4YAKXT4W 1) "pk" 2) "01GQ2XE5MH935FS5H2WTGEY9DG" 3) "name" 4) "Karan" 5) "age" 6) "28" 7) "email" 8) "[email protected]" 9) "about" 10) "I'm a Tech Geek"
By running the HGETALL command on that key, we can see that the hash was successfully saved in Redis, and we have retrieved it in the CLI.
In Redis, data expiration and deletion are essential concepts for managing the lifetime of key-value pairs. Expiration allows you to set a time limit on a key, after which it will automatically be deleted. This can be useful during caching. Deletion allows you to remove a key-value pair from the Redis database manually.
In Redis OM, data expiration can be achieved using the expire() method. The method takes in seconds two arguments, the key and the time. For example, to expire the karan data after 10 seconds, you can use the following code:
karan.expire(10)
After running the above code, an expiration of 10 seconds is set to our data model. And after 10 seconds have elapsed, the data is deleted from the Redis server.
Similar to this is the delete function. The argument provided is the PK. Calling the delete() function on the model deletes the model manually from the Redis database.
Student.delete(karan.pk)
After running the code above, the hash that stores data of karan is permanently deleted from the server.
In conclusion, Redis OM is a high-level API that allows you to interact with Redis in an object-oriented way. This simplifies data management in Redis and gives an easy approach to data interaction. Leveraging Redis OM, you may store, retrieve, and manipulate data in Redis more efficiently. So, we have seen some of the basic commands used to generate, save and retrieve data from the Redis server using the Redis-om client. Also, we have seen how to delete data and add an expiration instantly.
The key takeaways are:
To learn more about Redis-on, you can visit their official GitHub page. I hope you have enjoyed reading through the article.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.