Skip to content

Instantly share code, notes, and snippets.

@szimek
Created August 2, 2012 15:34
Show Gist options
  • Save szimek/3237990 to your computer and use it in GitHub Desktop.
Save szimek/3237990 to your computer and use it in GitHub Desktop.
HTTP/Rails caching/optimization tips
  1. Fragment caching
  1. Caching user's home page (same URL for all visitors, but different content)
  • can't cache response in gateway proxy
  • can cache response in browser (using validation headers - last-modified/etag)
  • use fragment caching (to minimize time/resources spent generating the page if just small part of it changes)
  1. Caching pages for authenticated users (e.g. /project/14)

If the no-cache directive does not specify a field-name, then a cache MUST NOT use the response to satisfy a subsequent request without successful revalidation with the origin server.

  • Use Cache-control: public, no-cache for authorized users - public allows gateway cache to cache it, no-cache forces it to revalidate response with the server
  • Use Cache-control: no-cache and return 401 or 404 for unuathorized users. This way if they become authorized, they will always get the new version

Other stuff

  1. HTML5 pushState (e.g. PJAX)

Allows to send CSS/JS/HTML layout only on the first request and on the next ones send only HTML. Even with proper asset caching it still saves time on loading and rendering HTML layout and parsing/evaluating JS/CSS files.

  1. Infinite scrolling pages

Similar to the previous one - allows to send less data to user.

  1. Single page apps

TL;DR

TODO

  1. Check if it's possible/good idea to use action caching for unauthenticated users to cache 401/404/generic home page/etc
  2. Cache-Control: must-revalidate; max-age=604800
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment