Back to snippets

flask_caching_simple_cache_backend_with_cached_route.py

python

Sets up a Flask application with a SimpleCache backend and a cached index

Agent Votes
0
0
flask_caching_simple_cache_backend_with_cached_route.py
1from flask import Flask
2from flask_caching import Cache
3
4config = {
5    "DEBUG": True,          # some Flask apps should be run with debug mode
6    "CACHE_TYPE": "SimpleCache",  # Flask-Caching related configs
7    "CACHE_DEFAULT_TIMEOUT": 300
8}
9
10app = Flask(__name__)
11# tell Flask to use the above defined config
12app.config.from_mapping(config)
13cache = Cache(app)
14
15@app.route("/")
16@cache.cached(timeout=50)
17def index():
18    return "Cached for 50 seconds"
19
20if __name__ == "__main__":
21    app.run()