Using caching with your application
Caching becomes an important and integral part of any web application when scaling or increasing the response time of your application becomes a question. Caching is the first thing that is implemented in these cases. Flask, by itself, does not provide any caching support by default, but Werkzeug does. Werkzeug has some basic support to cache with multiple backends, such as Memcached and Redis.
Getting ready
We will install a Flask extension called Flask-Cache, which simplifies the process of caching a lot:
$ pip install Flask-Cache
We will use our catalog application for this purpose and implement caching for some methods.
How to do it…
First, we need to initialize Cache
to work with our application. This is done in the application's configuration, that is, my_app/__init__.py
:
from flask.ext.cache 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...