Ryan Bates' render-caching gem is nice, but it keys cache entries by request_uri
s:
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.