GAE: Enabling Edge Cache with webapp2 (Python)

风格不统一 提交于 2019-12-03 16:41:23

This should be all you need:

self.response.cache_control = 'public'
self.response.cache_control.max_age = 300

Check Caching Details for more information, may be you broke some rules. Next the best part:

A response can be stored in Cloud CDN caches only if all of the following are true:

  • It was served by a backend service with caching enabled.
  • It was a response to a GET request.
  • The status code was 200, 203, 300, 301, 302, 307, or 410.
  • It has a Cache-Control: public directive.
  • It has a Cache-Control: s-maxage, Cache-Control: max-age, or Expires header.
  • It has either a Content-Length header or a Transfer-Encoding header.

Additionally, there are checks that will block caching of responses. A response will not be cached if any of the following are true:

  • It has a Set-Cookie header.
  • Its body exceeds 4 MB.
  • It has a Vary header with a value other than Accept, Accept-Encoding, or - Origin.
  • It has a Cache-Control: no-store, no-cache, or private directive.
  • The corresponding request had a Cache-Control: no-store directive.

I'm guessing that you're mixing up two related but distinct ideas.

The first idea, which the video you link to talks about, is arranging to have certain files in your app served by a pool of App Engine servers that specialize in serving static content. This is faster than having your app serve these files, since there won't be a delay to start up a new instance of your app to serve a static file. (Strongly consider serving up your .js and .css this way.) This static serving facility is controlled entirely at app update (upload) time, via declarations you make in app.yaml (or appengine-web.xml for Java apps).

The second idea is arranging, via HTTP response headers, for pages that your app emits to be cacheable by caches outside of app engine.

If you declare files as static, you have some control over addition HTTP response headers that get served along with the file. See the documentation on configuring static files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!