- implement a
#cache_key
method for every model, as this:
def cache_key
"#{self.class.model_name.cache_key}/#{id}-#{self[:updated_at].utc.to_s(:number)}"
end
- update
update_at
field whenever the model or it's related models (through method such astouch
) are changed.
So whenever the model's content is updated, #cache_key
update as well, e.g. from 'Event/5-2016033110450'
to 'Event/5-2016040125545'
.
When you ask for the cache, you do:
Rails.cache.find(@event.cache_key)
This will give you the newest copy of event.
- the
#cache_key
method is inspired by ActiveRecord's #cache_key method
FE will have to provide the key with timestamp in the request.