Book Image

Flask Framework Cookbook

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook

By: Shalabh Aggarwal

Overview of this book

Table of Contents (19 chapters)
Flask Framework Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Model data indexing with Redis


There might be some features that we want to implement but do not want to have a persistent storage for them. So, we would like to have these stored in a cache-like storage for a short period of time and then hide them, for example, showing a list of the recently visited products to the visitors on the website.

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 the app, or in the views file, where we will use it. It is preferred that you do this in the application file because then the connection will be open throughout...