Home > database >  How to Clear Dynamically Generated Page Cached on the Server
How to Clear Dynamically Generated Page Cached on the Server

Time:06-10

I'm generating a html page that is basically a list of photos saved in a single folder. I trigger the generation of the pictures.html template each time a new photo is added to the folder.

My problem is the old template page seems to be used from the cached even when I open a new browser on a different device which is what makes me think its a server and not a client cache.

I'm an old coder but new to dynamic web pages. I'm using Apache2 web server on a raspberry pi Buster (Debian) as my web server. I'm using Python3 to create the web pages. I found a way to edit the Apache config file and add a cache exclusion with an example like this one that excludes images.

<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
  Header set Cache-Control "max-age=63072000, public"
</FilesMatch>

The problem is that the "file" I want to exclude never seems to be an actual file. It's built from a template named pictures.html and some standard info etc is added.

If I understand correctly the code below creates and loads the page from my main file named app.py.

@app.route('/pictures/')
def pictures():
    return render_template('pictures.html')

I even checked the following day and the old page is still cached somewhere and loaded into the client browser.

Any ideas how I can force this page to be dropped from the server cache every time it is updated. Obviously the problem goes away if I restart the Apache service but that's not an option in production.

Even clearing the entire cache would do but that will probably kill a lot of performance gains for all the static files. My application regularly adds new photos to the folder.

Thanks for any ideas David

Update

Further research found this page I added the CacheDisable Directive to my apache2.conf file but the apache service now fails to start.

<Location "/pictures">
    CacheDisable on
</Location>

Here is the error message

Jun 01 18:25:52 ROVER1 apachectl[10723]: AH00526: Syntax error on line 214 of /etc/apache2/apache2.conf:
Jun 01 18:25:52 ROVER1 apachectl[10723]: Invalid command 'CacheDisable', perhaps misspelled or defined by a module not includ
Jun 01 18:25:52 ROVER1 apachectl[10723]: Action 'start' failed.
Jun 01 18:25:52 ROVER1 apachectl[10723]: The Apache error log may have more information.
Jun 01 18:25:52 ROVER1 systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE

CodePudding user response:

Turned out I was barking up the wrong tree :) I changed my approach and removed all the changeable file information from the pictures.html template. I then added code to app.py that reads the file information and saves it to a list. I then pass the list to the pictures.html template and in the html, iterated through the list loading the details into a table using jinja text. It means the list will be generated every time the page is loaded even when nothing has changed since last time but it seems more sensible that dynamically modifying a template and trying to avoid the cache. I Hope I have all the terminology correct and that this result helps someone else. David

  • Related