Forum: Ruby on Rails How key-based cache expiration works for has_many relationship?

49e98c4a13a5203ecff5d2d0774f8991?d=identicon&s=25 Matthijs Langenberg (mlangenberg)
on 2013-04-13 18:13
(Received via mailing list)
As an opportunity to get my latest project so damn fast, I looked into
key-based cache expiration, also known as the Russion Doll approach to
caching.

There is something I don't understand.

"You deal with dependency structures by tying the model objects together
on
> updates."


So if you change a todo that belongs to a todolist that belongs to a
> project, you update the updated_at timestamp on every part of the chain,
> which will automatically then update the cache keys based on these objects


Okay, so the hierarchy is project -> todolist -> todo. Then a single
todo
item, probably keeps track of its creator, right?

In Rails it could be declared like this:

class Project < ActiveRecord::Base
end

class Todolist < ActiveRecord::Base
  belongs_to :project, touch: true
end

class Todo < ActiveRecord::Base
  belongs_to :todolist, touch: true
  belongs_to :creator
end

# in app/views/project/show.html.erb
<% cache project do %>
  <p>All my todo lists:</p>
  <%= render project.todolists %>
<% end %>

# in app/views/todolists/_todolist.html.erb
<% cache todolist do %>
  <p><%= todolist.name %>:</p>
  <%= render todolist.todos %>
<% end %>

# in app/views/todo/_todo.html.erb
<% cache todo do %>
  <p><%= todo.name %>(by <%= todo.creator.name %>)</p>
<% end %>

# This will not trigger todo.touch!,
# which in turn does not trigger todo.todolist.touch!
# and does not trigger todo.todolist.project.touch!
todo.creator.update_attributes(name: 'John Doe')

What do I need to change in order to make this process trivial to
implement
caching schemes and trust that I am never going to serve stale data?
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.