0

I am using flask-cache (FileSystemCache)to store an entire table worth of data (to prevent constant database IO)

This works great, and really speeds up the reading of the records, but my app also allows users to "update rows" in the database.

I am fine with the IO in this case, however I would also like to update the local cache of the row in this situation (because if the user revisits the last updated row, the cache will be what was previously fetched from the database and will not reflect the most recent user update).

I can see the cache is generated and it is stored in some binary way (pickle?), which I can see contains all the rows (and as mentioned, the cache is working as expected for "reads"). I don't know how to either "get" or "set" specific rows within the cache file though.

Below is the simplified code of what I am doing:

@cache.cached(timeout=500, key_prefix='all_docs')
def cache_all_db_rows(table_name):
    engine = create_sql_alchemy_engine()
    connection = engine.connect()
    results = connection.execute(stmt).fetchall()
    return [row for row in results]

@site.route('/display/<doc_id>', methods=["GET", "POST"])
@login_required
def display(doc_id):
    form = CommentForm(request.form)    
    results = cache_all_db_rows(table_name)
    if request.method == "POST":
        if form.validate_on_submit():
            comments = form.comment.data
            relevant = form.relevant.data
            database_rate_or_add_comment(comments=comments, relevant_flag=relevant, doc_id=doc_id)

            # Ideally I would set the update to the cache here (after a successful db update)
            cache.set("foo", comments)

    return render_template("display.html", form = form)

I tried a few things, but can't seem to query the cache (pickle file?)... I tried adding code to query what is actually in the cache file by doing this:

            obj = []
            file_name = "./cache/dfaec33f482d83493ed6ae7e87ace5f9"
            with open(file_name,"rb") as fileOpener:
                while True:
                    try:
                        obj.append(pickle.load(fileOpener))
                    except EOFError:
                        break
            app.logger.info(str(obj))

but I am receiving an error: _pickle.UnpicklingError: invalid load key, '\xfb'.

I am not sure how to interact with the flask-cache.

Calamari
  • 29
  • 1
  • 9

0 Answers0