Plugins - HTTP Caching

StarAdd to favorites

http_caching plugin for Rails

Allows your Rails application to take advantage of the caching mechanisms built into HTTP 1.1 (i.e., 304 Not Modified return code). Inspired by a Rails cookbook entry (http://manuals.rubyonrails.com/read/chapter/62).

Installation

If your project is source-controlled by Subversion (which it should be, really), the easiest way to install this is via Rails’ plugin script:

  ./script/plugin install -x http://svn.codahale.com/http_caching

If you’re not using Subversion, or if you don’t want it adding svn:externals in your project, remove the -x switch:

  ./script/plugin install http://svn.codahale.com/http_caching

Usage

An example using send_data:

  def get_orders
    @photo = Photo.find(params[:id])
    if_cached_before @photo.updated_at do
      send_data @photo.data, :type => @photo.mime_type, :disposition => 'inline'
    end
  end

Another example, this time using a simple RHTML view:

  def get_user_profile
    @user = User.find(params[:id])
    render_if_cached_before @user.updated_at
  end

A slightly more complicated RHTML example:

  def get_orders
    render_if_cached_before Order.maximum(:updated_at), :layout => 'orders_feed', :action => 'all_orders'
  end

Or you can keep frequent refreshers from swamping your server:

  def shiny_object
    if_cached_before 10.minutes.ago do
      @shiny_object = ShinyObject.complicated_database_call(:woo => :hah)
    end
  end

You get the idea.

Resources

Subversion

Blog

Credits

Written by Coda Hale . Released under the MIT License for great justice.

Coda Hale

http://blog.codahale.com/2006/05/23/rails-plugin-http_caching/

http://svn.codahale.com/http_caching

Rails' (MIT)

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Misc. Enhancements

Tags

Comments

Add a comment

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?