GAE: Enabling Edge Cache with webapp2 (Python)

2019-03-30 20:23发布

There has been this new video on youtube demonstrating the strength of EdgeCaching in the GAE architecture, and at this particular point in the video they demonstrate how easy it is to leverage: http://www.youtube.com/watch?v=QJp6hmASstQ#t=11m12

Unfortunately it's not that easy...

I'm looking to enable edge caching using the webapp2 framework provided by Google.

I'm calling:

self.response.pragma = 'Public'
self.response.cache_expires(300)

but it seems overridden by something else.

The header I get is:

HTTP/1.1 200 OK
Pragma: Public
Cache-Control: max-age=300, no-cache
Expires: Sat, 23 Feb 2013 19:15:11 GMT
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
X-AppEngine-Estimated-CPM-US-Dollars: $0.000085
X-AppEngine-Resource-Usage: ms=39 cpu_ms=64
Date: Sat, 23 Feb 2013 19:10:11 GMT
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Cache-Control: no-cache, must-revalidate
Vary: Accept-Encoding
Server: Google Frontend
Content-Length: 600

I'm using ndb top level:

app = ndb.toplevel(webapp2.WSGIApplication(...

I tried the technics explained here, but they don't seem to apply to webapp2: http://code.google.com/p/googleappengine/issues/detail?id=2258#c14

I also looked at this post too: https://groups.google.com/d/topic/webapp2/NmHXoZZSVvo/discussion

I tried to set everything manually with no success. Something is overriding my cache settings.

Is there a way to make it work with webapp2? Any other option is welcome.

EDIT: I'm using an url with version prefix: http://version.appname.appspot.com and it's probably the cause of my problem.

3条回答
何必那么认真
2楼-- · 2019-03-30 20:40

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.

查看更多
别忘想泡老子
3楼-- · 2019-03-30 20:41

This should be all you need:

self.response.cache_control = 'public'
self.response.cache_control.max_age = 300
查看更多
姐就是有狂的资本
4楼-- · 2019-03-30 21:05

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.
查看更多
登录 后发表回答