Using fragment caching in Rails per user

caching, memcached, ruby-on-rails, ruby-on-rails-4

Solution

Do something like the following with the fragment cache key:

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

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

You can pass any number of items in the `name` argument of the cache method to cater the cache to your circumstance.

Problem

I have page which list all events. But listing of event depends on whether user is admin or not. If I use fragment caching on the page where I iterate through model object on view, it will cache all events for admin. Can it get served from cache to another user who is not admin? If yes, how I can use fragment caching for non-admin and admin user.

Original source