开发者

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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜