Book Image

Learning Flask Framework

Book Image

Learning Flask Framework

Overview of this book

Table of Contents (17 chapters)
Learning Flask Framework
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Caching with Flask-Cache and Redis


Sometimes, (and I know it's hard to imagine) we put a lot of effort into our sites, building in and adding features, and often that means we end up having to do a lot of database calls or complex template rendering for a page that is simply a static blog entry. Now database calls should not be slow and a lot of template renderings should not be noticeable but, if you expand that to lots of users (which hopefully you are expecting), this may become an issue.

So, if the site is mostly static why not store your response in a single, high-speed memory-based data store? No need for expensive database calls or complex template renderings; for the same input, or path, get the same content, and faster.

As is becoming a kind of a catch-phrase by now, we can already do this in Python and it is as simple as the following:

sudo pip install Flask-Cache

To get it running, add this to your app or your blueprint:

from flask.ext.cache import Cache

app = Flask(__name__)
cache...