| Rails ETags and Authentication | [September 14, 2011 | 4:22 PM] |
|---|
ETags are great for keeping the expensive stuff from executing over and over again, for example:
1 2 3 4 5 |
def some_action if stale?(:etag => some_obj, :last_modified => some_obj.created_at, :public => true) some_obj.some_really_expensive_operation end end |
Will prevents some_really_expensive_operation from executing a second time when the user revisits the page if nothing in the object has changed. This works great on public pages but what if you have some expensive stuff on a page that requires authentication, or a page that the content changes when a user logs in? Fortunately, any object can be passed for the :etag option. We could create a method called authenticated_stale? and include the current user as follows in the application controller as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
class ApplicationController < ActionController::Base #your code def current_user @current_user ||= User.find_by_id(session[:user_id] end def authenticated_stale?(options = {}) options[:etag] = [options[:etag], current_user] stale?(options) end end |
And use authenticated_stale? for any page that requires authentication or where the content changes if you are authenticated as you would the stale? method. Make sure not to set the public option true on authenticated pages so they don’t get cached by proxy caches.