Indexing model data with Redis
There may be some features we want to implement but do not want to have persistent storage for them. In such a use case, it is a good approach to have these stored in cache-like storage temporarily – for example, when we want to show a list of recently viewed products to visitors on a website. In this recipe, we will understand how to use Redis as an effective cache to store non-persistent data that can be accessed at a high speed.
Getting ready
We will do this with the help of Redis, which can be installed using the following command:
$ pip install redis
Make sure that you run the Redis server for the connection to happen. To install and run a Redis server, refer to http://redis.io/topics/quickstart.
Then, we need to have the connection open to Redis. This can be done by adding the following lines of code to my_app/__init__.py
:
from redis import Redis redis = Redis()
We can do this in our application file, where we will define...