开发者

Rails action/fragment cache only if admin user is not logged in

开发者 https://www.devze.com 2023-01-31 11:43 出处:网络
Is there a way to disable action/fragment caching when an \"admin\" user is logged in (like when current_user.role = \"admin\")?

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

精彩评论

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