Suppose I have User
model and Comment
model that belongs_to :user
假设我有belongs_to:user的用户模型和评论模型
I understand what to do when I want to cache user page with comments listed (via 'russian doll caching') - I do global cache
block for entire user view and put inside many cache blocks for each of user's comment, and add touch: true
for belongs_to :user
association. So, when some comment changes, there is only 2 caches to update - for that comment, and for that user.
我知道当我想要缓存列出了评论的用户页面时要做什么(通过'俄语玩偶缓存') - 我为整个用户视图执行全局缓存块,并为每个用户的注释放入许多缓存块,并添加touch:true for belongs_to:用户关联。因此,当某些注释发生更改时,只有2个缓存可供更新 - 用于该注释以及该用户。
But now I need to cache comment view that looks like:
但现在我需要缓存注释视图,如下所示:
= comment.user.name
= comment.text
So we need update cache not only when this comment changed, but when user name changed. What is more appropriate way to do this?
因此,我们不仅需要在此注释更改时更新缓存,还需要更改用户名。有什么更合适的方法呢?
My current solution is after save callback on user that makes comment.update_all(updated_at: updated_at)
and wrapping view in cache(comment)
block. But I don't like it because I want to detect only some fields change.
我当前的解决方案是在用户保存回调之后进行comment.update_all(updated_at:updated_at)并将视图包装在缓存(注释)块中。但我不喜欢它,因为我只想检测一些字段的变化。
Another solution is to wrap view in cache(comment, comment.user)
but I think it's worse because it does additional query for user.
另一种解决方案是将视图包装在缓存中(comment,comment.user),但我认为它更糟糕,因为它为用户执行了额外的查询。
Am I miss something obvious? What is your experience?
我想念一些明显的东西吗?你有什么经历?
1 个解决方案
#1
0
I think you must to use has_many :comments, autosave: true
in your User
model.
我认为你必须在你的用户模型中使用has_many:comments,autosave:true。
#1
0
I think you must to use has_many :comments, autosave: true
in your User
model.
我认为你必须在你的用户模型中使用has_many:comments,autosave:true。