views:

56

answers:

1

Ryan Bates' render-caching gem is nice, but it keys cache entries by request_uris:

def render_with_cache(key = nil, options = nil)
  key ||= request.request_uri     #  <----
  body = Rails.cache.read(key)
  if body
    render :text => body
  else
    yield if block_given?
    render unless performed?
    Rails.cache.write(key, response.body, options)
  end
end

This is an incomplete solution since what my application renders for a given URI varies based on:

  1. The current user
  2. The format of the request (html, js, atom)

How can I modify this method take the current user and request format into account?

+1  A: 

The method accepts a key argument, thus you don't need to hack it. Simply pass your cache name as argument.

render_with_cache([current_user.id, request.format, request.uri].join("/")) do
  # ...
end

If you often find yourself calling the method with this argument, creare a new method which wraps the previous one.

def render_with_cache_scoped_by_user(key = nil, options = nil, &block)
  scoped_key = [current_user.id, request.format, request.uri]
  scoped_key << key unless key.blank?
  render_with_cache(scoped_key.join("/"), options, &block)
end
Simone Carletti