jhlu87
jhlu87

Reputation: 4019

rails cache for each user

I want to have a cache for each user. So for example, I want to do something like this

<% cache [current_user, events] do %>
  <%= render events %>
<% end %>

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

but according to the documentation, updating either 'current_user' or 'events' will invalidate the cache. If I understand correctly, that means if two users are alternating hitting the cache, then the cache will be invalidated every time. Is it possible to create a caching system so each user will get his own cache and the caches will expire based on some other event like elapsed time or user sign out?

I only want the cache to be invalidated when events changes while the current_user field serves almost as a lookup for which cache to use. Is this possible? Thanks

Upvotes: 2

Views: 536

Answers (1)

Luca B.
Luca B.

Reputation: 648

The two users will already have different caches. The cache parameter is used to identify which cache rails should pull the data from. So that way you're already good to go.

Also, you prolly want to make it:

<% cache [current_user.id, events] do %>

So the cache for the said user will change only if events change (not, i.e. if user change his name)

Upvotes: 2

Related Questions