Setting Cache Control Headers In Python To Take Advantage Of Google AppEngine’s EdgeCache

To get the most speed out of Google AppEngine, and pay the least for hosting, you want to take advantage of the Google Edge Cache when ever possible.  I have fought back and forth with issues related to the Google EdgeCache… Sometimes it would work, sometimes it wouldn’t sometimes it would hold on to files way too long…

Well it hadn’t been working at all for like 6 weeks, and I didn’t mess with it, but I got around to it today.

If you want edge caching to work you need to make sure you have done the following things

1. Set Public If you don’t set public it won’t be cached ever.

2. Set a max-age . If you set public but don’t specify max-age it won’t be cached

3. Use a comma. public, max-age=300 works fine. Public; max-age=300 does not.

4. Set an age greater than 60. 61 seems to cache. 60 does not. There is probably some volume to will I cache based on expiration but 61 seconds at the volumes we run seems to cache and 60 doesn’t ever seem to.

5. Set an age less than 366 days. 364 days seems to work 365 works most the time 366 never seems to work. So those “Expire never” kinds of posts people talk about for versioned assets that never expire. Well 10 years is not the right answer.

6. Expires with a date, doesn’t seem to help, and seemingly may prevent caching in some instances. I think this may be clock drift. Or something about how picky the parser is about the format of the date. Things that work in browsers don’t always work correctly in the edgecache… (like the Semi vs. the comma)

7. Set both Pragma and Cache-Control If Pragma is not set Public then Cache-Control seems to be ignored.

This is all way more info than you probably need, but… Here is the relevant code.

Here is the python code to set headers for caching (CACHE_EXPIRE is a variable we set based on the page type)

self.response.headers[‘Cache-Control’] = ‘public, max-age=%d’ % CACHE_EXPIRE

self.response.headers[‘Pragma’] = ‘Public’

Hopefully this will save you time in your Python Coding. There is likely the equivalent in Java, we just didn’t need it so we didn’t document it.