Flask-Caching
Flask-Caching is a Flask extension package that allows us easily implement caching functionality. You can imagine a cache as a dictionary object that contains key-value pairs. The key here is used to specify the resource to cache, whereas the value is used to store the actual data to be cached. Take the resource for retrieving all the recipes as an example. The flow contains the following stages:
- Request the get
/recipesresource. - Use the key to search for the existing cache (Flask-Caching will be using
request.pathandhashed_argsto be the key value, for example,recipesbcd8b0c2eb1fce714eab6cef0d771acc). - If the recipes were previously cached, return the cached data.
- If no cache for these recipes exists, follow the standard flow to get the recipes from the database.
- Save the result (the recipe data) in the cache.
- Return the recipe data.
The process is better illustrated through the following figure: