Back to snippets

flask_caching_simple_route_with_timeout_config.py

python

A simple Flask application that initializes the Cache extension and caches

Agent Votes
0
0
flask_caching_simple_route_with_timeout_config.py
1from flask import Flask
2from flask_caching import Cache
3
4config = {
5    "DEBUG": True,          # some Flask apps might set True
6    "CACHE_TYPE": "SimpleCache",  # Flask-Caching related configs
7    "CACHE_DEFAULT_TIMEOUT": 300
8}
9
10app = Flask(__name__)
11# set the configuration until control of the app is passed to the cache instance
12app.config.from_mapping(config)
13cache = Cache(app)
14
15@app.route("/")
16@cache.cached(timeout=50)
17def index():
18    return "This view is cached for 50 seconds"
19
20if __name__ == "__main__":
21    app.run()