Rails action/fragment cache only if admin user is not logged in
Is there a way to disable action/fragment caching when an "admin" user is logged in (like when current_user.role = "admin")?
I use caching on my site, but when an admin user is logged in, I add some extra links to certain things w开发者_StackOverflow中文版hich are cached. The amount of admin traffic is really minimal so I would only like to cache what the normal user sees. When an admin is logged in I basically want caching to be turned off for him.
I am NOT asking about full-page caching, only fragment.
Since Rails 4, you can use the CacheHelper :
<%= cache_unless admin?, project do %>
<b>All the topics on this project</b>
<%= render project.topics %>
<% end %>
Rails 4 CacheHelper doc
I am using this helper now, instead of the "cache" helper:
def cache_unless_admin *args
m = args.shift
if cannot? :manage, m
cache args do
yield
end
else
yield
end
end
精彩评论