简体   繁体   中英

How to use flask-cache on a route with parameter

I have a flask app that retrieves data from a database. I would like to cache this data so I am not constantly requesting data from the database.

Currently I am using flask_caching and it is working, but ONLY for the individual document I request.

For example the database might contain 1000 rows, and I would like to cache ALL rows (not just one row, which is the behaviour now). I understand it is doing this, because the URL I am requesting specifies the doc_id.

My route in flask looks like this:

@site.route('/display/<doc_id>', methods=["GET", "POST"])
@cache.cached(query_string=True)
def display(doc_id):
    results = connect_to_db_get_ALL_results()
    return render_template("display.html")

The above code is simplified. I can see the cache IS working for a specific document (ie if I load the page http://localhost/display/1 , then click on the "Next" button on my rendered page to retrieve the next document ( http://localhost/display/2 ), it will retrieve it from the database (not the cache), but it will cache what it just received from the database because when I click "previous document" on the rendered page ( http://localhost/display/1 ), it will retrieve it from the cache.

Because my function "connect_to_db_get_ALL_results" actually returns all of the results I need for all docs.. What I would like to do is cache all of these documents (rather than just the current document in the URL). Such that if I decided to load http://localhost/display/994 it would select the data from the cache.

I have tried a few things (including using query_string and memoize , but neither of these things is what I need), I have also tried caching the function that returns ALL of the results, but this doesn't seem to work. Please help!

UPDATE: The way around this was to cache the function (that returns all results) rather than the route.

@site.route('/display/<doc_id>', methods=["GET", "POST"])
def display(doc_id):
    results = connect_to_db_get_ALL_results()
    return render_template("display.html")

@cache.cached(timeout=500, key_prefix='all_docs')
def connect_to_db_get_ALL_results()
    <code to fetch the data from the table>

The way around this was to cache the function (that returns all results) rather than the route.

@site.route('/display/<doc_id>', methods=["GET", "POST"])
def display(doc_id):
    results = connect_to_db_get_ALL_results()
    return render_template("display.html")

@cache.cached(timeout=500, key_prefix='all_docs')
def connect_to_db_get_ALL_results()
    <code to fetch the data from the table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM