在每个用户的Rails中使用片段缓存

Aja*_*jay 7 memcached caching ruby-on-rails ruby-on-rails-4

我有列出所有活动的页面.但是事件列表取决于用户是否是管理员.如果我在浏览模型对象的页面上使用片段缓存,它将缓存管理员的所有事件.

它可以从缓存服务到另一个非管理员用户吗?如果是,我如何为非管理员和管理员用户使用片段缓存.

oma*_*ous 4

使用片段缓存键执行类似以下操作:

<% cache [current_user.role, :events] do %>
  <b>All the Events based on the Current User's Role</b>
  <%= render events %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

http://api.rubyonrails.org/classes/ActionView/Helpers/CacheHelper.html#method-i-cache

您可以在缓存方法的参数中传递任意数量的项目name,以使缓存适合您的情况。