How to use cache-control with python in GAE?

2019-06-05 06:33发布

问题:

I'm choosing whether to enable cache header and what difference it will make. The current code is this and I wonder whether I should enable the caching and what it will do?

#seconds_valid = 8600
#self.response.headers['Cache-Control'] = "public, max-age=%d" % seconds_valid

self.response.headers['Cache-Control'] = 'no-cache'

Can I test what the difference is if I change the code to this

seconds_valid = 8600
self.response.headers['Cache-Control'] = "public, max-age=%d" % seconds_valid

Am I doing it the right way? What exactly is going to happen when I enable the cache? Will I still be able to update the page?

Thank you

回答1:

There is also AppEngine's caching reverse proxy / edge cache which may pick up your Cache-Control header if given a max-age and set to public like in your example. The edge cache is "best effort", meaning it is not 100% certain it will cache your response.

More information can be found here and here.



回答2:

Setting Cache-Control will make no difference to your application. This value is only used by web browser, caching is done only on client side, not on the server. Correct values for Cache-Control can reduce your server loads and save bandwidth because user agents will try to cache content locally but it has nothing to do with appengine.

If you are looking for server-side caching to improve response time and decrease database reads have a look at memcached. To use memcache optimally you might also need to search the internet for cache-invalidation strategies.