Forum: Ruby on Rails Rendering unique cached page per user in Fragment Caching

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
84bcd9fd8891e61a77898ae1c7af00f2?d=identicon&s=25 Ankur Kumar (Guest)
on 2014-12-23 14:44
(Received via mailing list)
Hi,

My app has devise authentication for every user. I integrated *fragment
caching* for one of my views which is accessible only on sign-in.

The cached page saved by one user is accessible by another user. How can
I
implement a unique key based caching so that cached pages are unique for
every user?

I read about some caching methods below but not sure how to
create/retrieve
a unique cached page.

http://api.rubyonrails.org/classes/ActionControlle...

Thanks.
--
Regards,
Ankur
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-12-23 14:50
(Received via mailing list)
On 23 December 2014 at 13:42, Ankur Kumar <specialankur@gmail.com>
wrote:
>
> Hi,
>
> My app has devise authentication for every user. I integrated fragment
> caching for one of my views which is accessible only on sign-in.
>
> The cached page saved by one user is accessible by another user. How can I
> implement a unique key based caching so that cached pages are unique for
> every user?

Include the user id in the key string.

Colin
84bcd9fd8891e61a77898ae1c7af00f2?d=identicon&s=25 Ankur Kumar (Guest)
on 2014-12-30 14:35
(Received via mailing list)
Thanks Colin for your reply. Can you please let me know how can user_id
be
included in caching key string? I have following implementation of
caching
as of now.

app/views/employee/index.html.erb

<% cache "employee_list "do %>
  List of all employees:
  <% Employee.all.each do |e| %>
    <%= link_to e.name, employee_url(e) %>
  <% end %>
<% end %>

app/sweepers/employee_sweeper.rb

class EmployeeSweeper < ActionController::Caching::Sweeper
  observe Employee
  def sweep(employee)
   expire_fragment("employee_list")
 end
alias_method :after_create, :sweep
  alias_method :after_update, :sweep
  alias_method :after_destroy,:sweep
end

Devise "current_user" is accessible in all the above files.

--
Regards,
Ankur
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-12-30 16:34
(Received via mailing list)
On 30 December 2014 at 13:33, Ankur Kumar <specialankur@gmail.com>
wrote:
>
> Thanks Colin for your reply. Can you please let me know how can user_id be
> included in caching key string? I have following implementation of caching
> as of now.
>
> app/views/employee/index.html.erb
>
> <% cache "employee_list "do %>

Something like
cache "employee_list #{current_user.id}" do
so the user id is included in cache key.  Similarly for expire.

Colin
This topic is locked and can not be replied to.