Upgrade to Pro — share decks privately, control downloads, hide ads and more …

full page caching in general and Django

full page caching in general and Django

Denis Cornehl

May 19, 2015
Tweet

More Decks by Denis Cornehl

Other Decks in Technology

Transcript

  1. expiration • Cache-Control: max-age=N • Expires (for your CDN) •

    Surrogate-Control: max-age=N • Cache-Control: s-max-age=N
  2. cache key https://api.thermondo.de/settings/ • method (GET) • path (/api/settings/) •

    GET arguments (most times) • Language • Encoding • more?
  3. cache key https://api.thermondo.de/settings/ • method (GET) • path (/api/settings/) •

    GET arguments (most times) • Language • Encoding • User Session
  4. vary $ http -h https://api.thermondo.de/settings/ „Authorization: Token secrettoken" HTTP/1.1 200

    OK Allow: GET, HEAD, OPTIONS Api-Version: 1.0.0 Connection: keep-alive Content-Encoding: gzip Content-Language: en-us Content-Length: 646 Content-Type: application/json Date: Mon, 30 Mar 2015 12:48:31 GMT Etag: "9987fab5ec7adb7c70be2a24e3665a5b;gzip" Server: waitress Vary: Accept, Accept-Language, Cookie, Accept-Encoding Via: 1.1 vegur
  5. vary $ http -h https://api.thermondo.de/settings/ „Authorization: Token secrettoken" HTTP/1.1 200

    OK Allow: GET, HEAD, OPTIONS Api-Version: 1.0.0 Connection: keep-alive Content-Encoding: gzip Content-Language: en-us Content-Length: 646 Content-Type: application/json Date: Mon, 30 Mar 2015 12:48:31 GMT Etag: "9987fab5ec7adb7c70be2a24e3665a5b;gzip" Vary: Accept, Accept-Language, Cookie, Accept-Encoding Via: 1.1 vegur
  6. the best way (tm) • unique filenames + far future

    expiration (1 year) or • normal filenames, short expiration (5 min) or • normal filenames, CDN only expiration
 (and flush the CDN after changes)
  7. validation • CommonMiddleware • adds an ETag to the response

    if there is none • Conditional GET for ETag • generates new 304 response if ETag matches
 (keeps cookies)
  8. validation • ConditionalGetMiddleware • Conditional GET for ETag and Last-Modified

    • changes the response status code to 304 if conditions match
  9. validation • @etag, @last_modified, @condition • callables for ETag or

    Last-Modified calculation • best server performance • most RFC compliant
  10. expiration • FetchFromCacheMiddleware has to be last: 
 after everything

    that modifies the Vary-Header • UpdateCacheMiddleware has to be first: 
 before everything that modifies the Vary-Header
  11. vary • SessionMiddleware adds Cookie • GZipMiddleware adds Accept-Encoding •

    LocaleMiddleware adds Accept-Language • @vary_on_cookie, @vary_on_headers
  12. expiration breaks • when you use sessions • when you

    use CSRF protection (which uses cookies) • when you use google analytics
  13. possible but hard way • Middleware to filter google analytics

    cookies and add them back later 
 they have no effect on the content • be very careful with using the session / user in your views • be very careful with using forms (which use CSRF Protection)
  14. other ways • don’t use normal expiration headers • only

    flushable store (local cache, CDN) • flush the cache (for example) when the database changes