Caching view function in flask when it is also called directly with flask-caching

29 Views Asked by At

I have a flask application where I have some frontend views and some api views which are also called to render the frontend directly by the app, something like the example that follows:

@app.route("/stop/<int:stop_id>")
def stop(stop_id):
    stop_info = stop_api(stop_id)  # This function asks stop_api for the data to render the template
    return render_template("stop.html", data=stop_info)

@app.route("/api/stop/<int:stop_id>")
@cache.cached(timeout=20)
def stop_api(stop_id):
    return 'stop-info'  # This function goes and gets the data to the return it

This way when the api function is called directly through its route by a client, the result is cached, but when it is called by the stop function, it is run every time, and thus, not cached.

I could cache the stop function, but that wouldn't be too convenient, as it has other things that can also change, so I'd like to know if there is an easy way to make it also cached when called from another function as well as from its route.

0

There are 0 best solutions below