librelist archives

« back to archive

Cache Plugin

Cache Plugin

Max Sadrieh
2012-04-12 @ 18:08
Hi all,

Just a quick e-mail to the list to mention a small plugin I created
for nesta. It lets you add cache control headers to the HTTP responses
back from your server. If you're a bit confused, bear with me.

# HTTP Background

In a nutshell, when a user tries to access your page, CSS stylsheets,
and images, it creates a "GET" request asking for the resource. The
server replies with a "200 OK", your page itself and some metadata
about the resource (for example the type of the file, such as
text/html or text/css, or the last time it was modified).

Some pages change very often and it is necessary for the user to go
through this whole procedure every time (to get the latest news from for example). But some pages and resources almost never
change and it would be great to be able to tell the client to cache
the page, image, etc. and to simply use that version for x amount of
time. A great example is the GPL [1]: it will probably always be at
this address and the 3rd version won't ever change, therefore if
somebody has already accessed that document, it makes sense for his or
her browser to avoid generating a "GET" and waiting for a response.

This is done through the "Cache-control" and "Expires" headers. We
tell the browser the amount of time it can keep reusing its cached
version without getting it from the server. This is also useful for
proxies (they can return the page they cached to browsers without
having to proxy the request itself).


# Usage

The plugin allows you to specify values for maximum age of a cached
page and whether the caching should be public or private.

*Caution*: here public and private simply mean that we allow or not
proxy servers that might serve many users to cache the page. If the
page is personalized for each user, we don't want the proxies to cache
the page. This has nothing to do with security, encryption,
authentication, or anything like that.

In your config.yml you can specify the expires and expires_type settings:
  expires: 3600
  expires_type: public

In each page, you can specify it as metadata:
  Expires: 100
  Expires Type: public

The per-page value overrides the default value from config.yml

# Installation

  gem 'nesta-plugin-cache'
to your Gemfile and run `bundle`.

# More info

See the github page:

I more than welcome contributions, especially to make the plugin
easier to use for users that are not familiar with the joy of HTTP
cache directives!

-Max Sadrieh