2

I've tried a lot of things, and they all failed. My Django (2.0) website has some pages which take a lot of time to generate. I want to keep this pages in the database cache of the Django server until the database changed. My goal:

  • Have pages ready in database cache
  • Serve users the cached pages
  • Don't save these pages as cache in the browser (the browser can't know if the page needs to be re-generated)
  • Keep .js files etc as cache in browser
  • Don't use browser cache when using the browser 'back' button to go back to the calculation heavy page

The closest I got was to enable database caching, enabled per-site caching, and using cache.clear() on receiving post_save and post_delete signals. But still, if I pressed 'back' in my browser, the local cache was used (so no request was send to the server). I tried to fix this by adding @never_cache to the view, but this also prevented caching in the middleware...

1 Answers1

1

Hi,I encountered a similar situation, when I used cache_page in Django,


urlpatterns = [
    path(
        'xxx',
        cache_page(
            settings.CACHE_TTL, key_prefix='xxx'
        )(xxx),
        name='xxx'
    ),
]

it will add one response header in the response:

cache-control: max-age=600

Then the browser will not request new data even my cache data have changed in 10 minute.

My solution is change the response header at nginx:

location ~ (/xxx|/yyy) {
    # ...
    proxy_hide_header Cache-Control;
    add_header Cache-Control no-store;
    
    # ...
}