October 2018
Intermediate to advanced
332 pages
8h 9m
English
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 ...