elelias
elelias

Reputation: 4771

deleting Flask memory cache

I have a web app that starts like this:

#It starts here
@app.route('/game')
def game():

Inside game, it dynamically builds an image with PIL and saves it to /static/testing_image.png

It then renders a template.

return render_template('game.html',
                           file_name=file_name,
                           image_width=SQ_SIZE,
                           sleep_time=1000
                           )

Now, inside game.html, what happens is that I wait for sleep_timems and then I want to go call game again, so that a new image is built and displayed.

The code below waits until the image is fully loaded, it displays it, waits, and redirects.

function LoadImage() {
     var img = new Image(),
      x = document.getElementById("image");

    img.onload = function() {
        x.src = img.src;
        var sleep_time={{sleep_time}};
        setTimeout(function(){
            //I REDIRECT HERE
            window.location="{{url_for('game')}}";
         }, sleep_time);

     };

    img.src="{{url_for('static', filename=file_name)  }}";
}

THE PROBLEM

The issue is of course that the image is served from memory cache, and even though the underlying file, /static/testing_image.png, has been updated, flask does not get the new one.

enter image description here

You can see in the below how testing_image.png is served from the cache.

My question is two-fold.

  1. Should I be doing this in a different way?
  2. If not, how can I force flask to do a hard reload on the image?

Upvotes: 0

Views: 2651

Answers (1)

Kevin Schellenberg
Kevin Schellenberg

Reputation: 845

There is probably a better way to structure this, but it's hard to say without looking at the rest of your code. As far as busting the cache, you should be able to use this:

http://flask.pocoo.org/snippets/40/

Upvotes: 1

Related Questions