Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

How to do it...

Implementing basic caching is pretty easy. Follow these steps to do so:

  1. First, initialize Cache to work with our application. This is done in the application's configuration, that is, my_app/__init__.py:
from flask_caching import Cache 
 
cache = Cache(app, config={'CACHE_TYPE': 'simple'}) 

Here, we used simple as the Cache type, where the cache is stored in the memory. This is not advised for production environments. For production, we should use something such as Redis, Memcached, filesystem cache, and so on. Flask-Cache supports all of them with a couple of more backends.

  1. Next, add caching to the methods that need to be cached. Just add a @cache.cached(timeout=<time in seconds>) decorator to the view methods. A simple target ...
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

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content