加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 缓存有限的关联

发布时间:2020-12-17 03:21:26 所属栏目:百科 来源:网络整理
导读:我正在尝试使用其关联缓存ActiveRecord.问题是访问检索记录上的关联时存在数据库查询. 通常情况下,我只是用急切加载的Rails.cache.write(‘post’,Post.includes(:comments).find(99))进行缓存.这似乎有效,但问题是我只想缓存关联的有限子集,并且在急切加载
我正在尝试使用其关联缓存ActiveRecord.问题是访问检索记录上的关联时存在数据库查询.

通常情况下,我只是用急切加载的Rails.cache.write(‘post’,Post.includes(:comments).find(99))进行缓存.这似乎有效,但问题是我只想缓存关联的有限子集,并且在急切加载时忽略限制(例如,提到here).所以Post.includes(:popular_comments).find(99)将返回所有评论,而不仅仅是流行评论.

所以我在懒惰加载关联后尝试缓存对象,但是在拉出对象时不幸发生了一个查询:

class Post < ActiveRecord::Base
  has_many :comments
  has_many :popular_comments,:class_name > 'Comment',:limit => 20,:order => :votes

post = Post.find(99)
post.popular_comments # lazy-load limited associations
Rails.cache.write('post',post)
...
Rails.cache.read('post').popular_comments # Unwanted SQL query :(

我尝试过缓存克隆,同样不需要的SQL查询.我已经尝试使用redis和memcached实现,结果相同.奇怪的是,这个序列确实可以在控制台上运行,但在控制器或上面的视图中的简单用法失败(即SQL发生).

更新(2017年4月):我现在说这是一个愚蠢的前提.缓存整个对象通常是浪费的,因为它使用了大量的缓存存储,并且序列化/反序列化很慢.缓存关联(如本问题中所述)将该浪费乘以N.通常,只缓存原始ID和HTML片段会更有效.

解决方法

试试post.popular_comments.reload

首先,当急切加载时,实际上忽略了限制.从the docs开始:

If you eager load an association with a specified :limit option,it will be ignored,returning all the associated objects

这意味着,就像您发现的那样,您必须自己强制关联到父对象.在我的实验中,post.popular_comments不起作用(这是有道理的,因为它返回一个代理对象),有趣的是post.popular_comments.all也没有. post.popular_comments(true)可以解决问题.在该代码下面调用reload,并且简单地执行post.popular_comments.reload也会将关联加载到父类中.

我不确定这两个中哪一个更正确,post.popular_comments(true)或post.popular_comments.reload.两者看起来都有点脆弱,但第二个读得更好,更清楚地表达了你的意图.

我验证了这两种方法:

>在memcache中存储有限的关联
>从缓存加载后没有触发SQL查询

我的脚本存储帖子:

require 'pp'
Rails.cache.clear
post = Post.first

#post.popular_comments(true)
post.popular_comments.reload

Rails.logger.info "writing to cache"
s = Rails.cache.write "post",post
Rails.logger.info "writing to cache done"

并检索:

require 'pp'
Rails.logger.info "reading from cache"
post = Rails.cache.read "post"
Rails.logger.info "reading from cache done"
Rails.logger.info post.popular_comments.inspect

如果我一个接一个地运行我的日志显示:

Post Load (0.5ms)  SELECT `posts`.* FROM `posts` LIMIT 1
  Comment Load (0.5ms)  SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` = 1 ORDER BY votes LIMIT 20
writing to cache
writing to cache done
reading from cache
reading from cache done
[#<Comment id: 1,...

我的mySQL日志还确认第二个脚本不会触发关联查询.

这是通过Rails 3.1.1完成的

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读