开发者

Cache the output of an action on a per-format & per-user basis

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 un开发者_Python百科less 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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜