Book Image

Mastering Flask

By : Jack Stouffer
Book Image

Mastering Flask

By: Jack Stouffer

Overview of this book

Starting from a simple Flask app, this book will walk through advanced topics while providing practical examples of the lessons learned. After building a simple Flask app, a proper app structure is demonstrated by transforming the app to use a Model-View-Controller (MVC) architecture. With a scalable structure in hand, the next chapters use Flask extensions to provide extra functionality to the app, including user login and registration, NoSQL querying, a REST API, an admin interface, and more. Next, you’ll discover how to use unit testing to take the guesswork away from making sure the code is performing as it should. The book closes with a discussion of the different platforms that are available to deploy a Flask app on, the pros and cons of each one, and how to deploy on each one.
Table of Contents (20 chapters)
Mastering Flask
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Flask Cache


In Chapter 7, Using NoSQL with Flask, we learned that page load time is one of the most important factors to determine the success of your web app. Despite the fact that our pages do not change very often and due to the fact that new posts will not be made very often, we still render the template and query the database every single time the page is asked for by our user's browsers.

Flask Cache solves this problem by allowing us to store the results of our view functions and return the stored results rather than render the template again. First, we need to install Flask Cache from pip:

$ pip install Flask-Cache

Next, initialize it in extensions.py:

from flask.ext.cache import Cache

cache = Cache()

Then, register the Cache object on the application, in the create_app function in __init__.py:

from .extensions import (
    bcrypt,
    oid,
    login_manager,
    principals,
    rest_api,
    celery,
    debug_toolbar,
    cache
)

def create_app(object_name):
    …
    cache.init_app...