Rails Plugin: http_caching
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 (manuals.rubyonrails.com/read/chapter/62). If it fits into your app’s architecture, HTTP caching can make a real difference in user-perceived speed. “And it must pop pop pop!”
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])
unless_cached_after @photo.updated_at do
send_data @photo.data, :type => @photo.mime_type, :disposition => 'inline'
end
end
If the photo hasn’t been updated since the last time the client requested it, the photo isn’t sent, saving you time and bandwidth.
Another example, this time using a simple RHTML view:
def get_user_profile
@user = User.find(params[:id])
render_unless_cached_after @user.updated_at
end
A slightly more complicated RHTML example:
def get_orders
render_unless_cached_after Order.maximum(:updated_at), :layout => 'orders_feed', :action => 'all_orders'
end
Or you can keep frequent refreshers from swamping your server:
def shiny_object
unless_cached_after 10.minutes.ago do
@shiny_object = ShinyObject.complicated_database_call(:woo => :hah)
end
end
You get the idea.
(And yes, all I do is make Rails plugins.)
3 comments »