Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GAE: Enabling Edge Cache with webapp2 (Python)

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.

like image 640
sanx Avatar asked Dec 26 '22 08:12

sanx


2 Answers

This should be all you need:

self.response.cache_control = 'public'
self.response.cache_control.max_age = 300
like image 98
Greg Avatar answered Dec 31 '22 14:12

Greg


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.
like image 22
Daniel De León Avatar answered Dec 31 '22 15:12

Daniel De León