Skip to Content
Mastering Flask Web Development - Second Edition
book

Mastering Flask Web Development - Second Edition

by Daniel Gaspar, Jack Stouffer
October 2018
Intermediate to advanced
332 pages
8h 9m
English
Packt Publishing
Content preview from Mastering Flask Web Development - Second Edition

Caching views and functions

In order to cache the results of a view function, simply add a decorator to any function:

...from .. import cache...@blog_blueprint.route('/')@blog_blueprint.route('/<int:page>')@cache.cached(timeout=60)def home(page=1):    posts =     Post.query.order_by(Post.publish_date.desc()).paginate(page,     current_app.config['POSTS_PER_PAGE'], False)    recent, top_tags = sidebar_data()    return render_template(        'home.html',        posts=posts,        recent=recent,        top_tags=top_tags    )

The timeout parameter specifies how many seconds the cached result should last, before the function should again be run and stored. To confirm that the view is actually being cached, check the SQLAlchemy section of the Debug toolbar. Also, we can see the impact that ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Flask Web Development, 2nd Edition

Flask Web Development, 2nd Edition

Miguel Grinberg
Flask Web Development

Flask Web Development

Miguel Grinberg

Publisher Resources

ISBN: 9781788995405Supplemental Content