Applying caching using Flask-Caching
Flask-Caching provides enhanced caching support for views, data rendered by Jinja2 templates, and the custom functions, such as repository or service functions, of any Flask application. It also supports built-in caching of Flask and allows you to customize a cache through the BaseCache
class from its flask_caching.backends.base
module.
Before we can configure Flask-Caching, we must install the flask-caching
module via the pip
command:
pip install flask-caching
Then, we must register some of its configuration variables in the configuration file, such as CACHE_TYPE
, which sets the cache type suited for the application, and CACHE_DEFAULT_TIMEOUT
, which sets the caching timeout. The following are the applications’ caching configuration variables declared in their respective config-dev.toml
files:
CACHE_TYPE = "FileSystemCache" CACHE_DEFAULT_TIMEOUT = 300 CACHE_DIR = "./cache_dir/" CACHE_THRESHOLD = 800
Here,...