Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm building a Python/Flask Application using a MySQL backend. Many of the views have forms that submit data to a route, and then the route adds the data via db.session.commit(). I can confirm that the MySQL db updates with every commit, but then if I subsequently refresh the page multiple times, the data changes every time (i.e. most recently added items will disappear, reappear).

I've done a couple things to try to fix:

HTML Headers
I know it's not best to add a ton of headers and hope for the best, but I added these headers to fight caching:

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="private, proxy-revalidate, s-maxage=0, no-cache, no-store, must-revalidate" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

Flask Headers I added headers to the Flask app itself:

response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.
response.headers["Pragma"] = "no-cache" # HTTP 1.0.
response.headers["Expires"] = "0" # Proxies.

Disable MySQL Caching

SET SESSION query_cache_type=OFF;

Apache2 Disable Caching

<filesMatch "\.(html|htm|js|css)$">
    FileETag None
    <ifModule mod_headers.c>
            Header unset ETag
            Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
            Header set Pragma "no-cache"
            Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    </ifModule>
</filesMatch>

Some behaviors based on just prying around and facts:

  • Whenever I execute sudo service apache2 restart, following it everything works fine. Some other posts mentioned that there may be processes that are started that are causing this?
  • I'm using SQLAlchemy as my ORM, so at one point I thought that maybe it was SQLAlchemy caching information, so following every commit I tried db.session.close() so that the connection would be fresh every time I queried.

Can anyone give me a hand? Like many folks learning Flask, I'm a beginner at web application development.

Many thanks!

EDIT:

  • When I run the application by just doing python init.py, it works perfectly fine.
  • I receive intermittent HTTP 500 errors when refreshing the page at a quick pace.

EDIT 2: Here's a sample of a route that I'm POSTing to, following which I'm redirecting to itself and displaying the GET option to the user. I would expect that after committing data, the subsequent query should include it, but many times the web application doesn't show the most recently added data. On multiple refreshes, it'll sometimes show and sometimes disappear. Very strange:

@app.route('/projects', methods=['GET','POST'])
@login_required
def projects():
    user = return_current_user()
    if request.method == 'POST':
            if request.form['title'] and request.form['description']:
                    project = Projects(request.form['title'], request.form['description'])
                    if project.title != None and project.description != None:
                            user.projects.append(project)
                            db.session.commit()

            return redirect('/projects')
    elif request.method == 'GET':
            if 'clear-page' in request.args:
                    return redirect('/projects')
            elif 'query' in request.args:
                    projects = Projects.query.filter(Projects.title.like('%'+request.args['query']+'%')).order_by(Projects.timestamp.desc()).all()

            else:
                    projects = Projects.query.order_by(Projects.timestamp.desc()).all()

            return render_template('projects.html', title="Projects", user=user, projects=projects)
share|improve this question

2 Answers 2

You might try switching off all the special changes you have made on the apparent assumption that some caching mechanism is responsible for the observed results, and show some of the code of your flask site. Then start to gather documented evidence of what is actually happening.

share|improve this answer
    
Thanks! I went ahead and disabled everything. The code base is available on GitHub, but I can hone in on a specific route as an example - will edit my initial question. –  jayrav13 Aug 21 at 21:58

I actually figured this out - a solve was to add db.session.commit() before the query. Not sure if it's the "correct" way to approach but it does the trick for my small-scale application!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.