1

我正在开发一个使用俄罗斯娃娃缓存的 Rails (4.1.14) 站点,如下所示:

home.html.erb:

<%= render partial: 'product/group', locals: {products: @products} %>`

产品/_group.html.erb:

<% products.each do |product| %>
  <%= render partial: 'product/big', locals: {product: product} %>`
  <%= render partial: 'product/small', locals: {product: product} %>`
<% end %>

产品/_big.html.erb:

<%= product.name %>`
<%= render partial: 'product/big_image', locals: {product: product} %>`

等等。

由于某种原因,此页面在开发中需要很长时间才能加载(12 秒!)。

当我将所有嵌套的部分内联到最外层时,页面加载下降了 75%,只有 3 秒。

这看起来很疯狂,因为它与渲染到屏幕上的内容完全相同,并且最外层的缓存应该使它与我如何设计里面的内容无关。

那么到底是什么导致了这种放缓呢?

我唯一能想到的是 Rails 计算内层的缓存摘要。我想在生产中,Rails 知道代码不会更改(?),因此它不会递归获取摘要?

有任何想法吗?

编辑:

我什至尝试将最外面的 ( product/group) 片段设置为简单<% cache do %>,以便所有组都呈现完全相同。该网站仍然需要大约 6 秒才能加载,并且日志显示了一系列活动:

  Cache digest for site/app/views/home/_product_big.html.erb: 336d5ebc5436534e61d16e63ddfca327
  Cache digest for site/app/views/shared/_discount.html.erb: 3c2aa3685fcb60f7d94e19d84263da77
  Cache digest for site/app/views/shared/_sale.html.erb: cb063c33aee387ae346ffe0d1679e7d9
  Cache digest for site/app/views/products/_image.html.erb: 50653bfa64782626a81e769fddc545ac
  Cache digest for site/app/views/home/_product_small.html.erb: f2c5bb4035a8b4261a2b6446ecad09ec
  Cache digest for site/app/views/home/_product_group.html.erb: 9c5de02f4004079ae10edd019083b659
Read fragment views/localhost:3000//9c5de02f4004079ae10edd019083b659 (1.3ms)
  Rendered site/app/views/home/_product_group.html.erb (531.8ms)
  Cache digest for site/app/views/home/_product_big.html.erb: 336d5ebc5436534e61d16e63ddfca327
  Cache digest for site/app/views/shared/_discount.html.erb: 3c2aa3685fcb60f7d94e19d84263da77
  Cache digest for site/app/views/shared/_sale.html.erb: cb063c33aee387ae346ffe0d1679e7d9
  Cache digest for site/app/views/products/_image.html.erb: 50653bfa64782626a81e769fddc545ac
  Cache digest for site/app/views/home/_product_small.html.erb: f2c5bb4035a8b4261a2b6446ecad09ec
  Cache digest for site/app/views/home/_product_group.html.erb: 9c5de02f4004079ae10edd019083b659
Read fragment views/localhost:3000//9c5de02f4004079ae10edd019083b659 (0.1ms)
  Rendered site/app/views/home/_product_group.html.erb (472.9ms)
  Cache digest for site/app/views/home/_product_big.html.erb: 336d5ebc5436534e61d16e63ddfca327
  Cache digest for site/app/views/shared/_discount.html.erb: 3c2aa3685fcb60f7d94e19d84263da77
  Cache digest for site/app/views/shared/_sale.html.erb: cb063c33aee387ae346ffe0d1679e7d9
  Cache digest for site/app/views/products/_image.html.erb: 50653bfa64782626a81e769fddc545ac
  Cache digest for site/app/views/home/_product_small.html.erb: f2c5bb4035a8b4261a2b6446ecad09ec
  Cache digest for site/app/views/home/_product_group.html.erb: 9c5de02f4004079ae10edd019083b659
Read fragment views/localhost:3000//9c5de02f4004079ae10edd019083b659 (0.1ms)
  Rendered site/app/views/home/_product_group.html.erb (521.7ms)
  Cache digest for site/app/views/home/_product_big.html.erb: 336d5ebc5436534e61d16e63ddfca327
  Cache digest for site/app/views/shared/_discount.html.erb: 3c2aa3685fcb60f7d94e19d84263da77
  Cache digest for site/app/views/shared/_sale.html.erb: cb063c33aee387ae346ffe0d1679e7d9
  Cache digest for site/app/views/products/_image.html.erb: 50653bfa64782626a81e769fddc545ac
  Cache digest for site/app/views/home/_product_small.html.erb: f2c5bb4035a8b4261a2b6446ecad09ec
  Cache digest for site/app/views/home/_product_group.html.erb: 9c5de02f4004079ae10edd019083b659
Read fragment views/localhost:3000//9c5de02f4004079ae10edd019083b659 (0.1ms)
  Rendered site/app/views/home/_product_group.html.erb (492.3ms)
  Cache digest for site/app/views/home/_product_big.html.erb: 336d5ebc5436534e61d16e63ddfca327
  Cache digest for site/app/views/shared/_discount.html.erb: 3c2aa3685fcb60f7d94e19d84263da77
  Cache digest for site/app/views/shared/_sale.html.erb: cb063c33aee387ae346ffe0d1679e7d9
  Cache digest for site/app/views/products/_image.html.erb: 50653bfa64782626a81e769fddc545ac
  Cache digest for site/app/views/home/_product_small.html.erb: f2c5bb4035a8b4261a2b6446ecad09ec
  Cache digest for site/app/views/home/_product_group.html.erb: 9c5de02f4004079ae10edd019083b659
Read fragment views/localhost:3000//9c5de02f4004079ae10edd019083b659 (0.1ms)
  Rendered site/app/views/home/_product_group.html.erb (512.0ms)
  Cache digest for site/app/views/home/_product_big.html.erb: 336d5ebc5436534e61d16e63ddfca327
  Cache digest for site/app/views/shared/_discount.html.erb: 3c2aa3685fcb60f7d94e19d84263da77
  Cache digest for site/app/views/shared/_sale.html.erb: cb063c33aee387ae346ffe0d1679e7d9
  Cache digest for site/app/views/products/_image.html.erb: 50653bfa64782626a81e769fddc545ac
  Cache digest for site/app/views/home/_product_small.html.erb: f2c5bb4035a8b4261a2b6446ecad09ec
  Cache digest for site/app/views/home/_product_group.html.erb: 9c5de02f4004079ae10edd019083b659
Read fragment views/localhost:3000//9c5de02f4004079ae10edd019083b659 (0.1ms)
  Rendered site/app/views/home/_product_group.html.erb (564.9ms)
  Cache digest for site/app/views/home/_product_big.html.erb: 336d5ebc5436534e61d16e63ddfca327
  Cache digest for site/app/views/shared/_discount.html.erb: 3c2aa3685fcb60f7d94e19d84263da77
  Cache digest for site/app/views/shared/_sale.html.erb: cb063c33aee387ae346ffe0d1679e7d9
  Cache digest for site/app/views/products/_image.html.erb: 50653bfa64782626a81e769fddc545ac
  Cache digest for site/app/views/home/_product_small.html.erb: f2c5bb4035a8b4261a2b6446ecad09ec
  Cache digest for site/app/views/home/_product_group.html.erb: 9c5de02f4004079ae10edd019083b659
Read fragment views/localhost:3000//9c5de02f4004079ae10edd019083b659 (0.2ms)
  Rendered site/app/views/home/_product_group.html.erb (484.1ms)
  Cache digest for site/app/views/home/_product_big.html.erb: 336d5ebc5436534e61d16e63ddfca327
  Cache digest for site/app/views/shared/_discount.html.erb: 3c2aa3685fcb60f7d94e19d84263da77
  Cache digest for site/app/views/shared/_sale.html.erb: cb063c33aee387ae346ffe0d1679e7d9
  Cache digest for site/app/views/products/_image.html.erb: 50653bfa64782626a81e769fddc545ac
  Cache digest for site/app/views/home/_product_small.html.erb: f2c5bb4035a8b4261a2b6446ecad09ec
  Cache digest for site/app/views/home/_product_group.html.erb: 9c5de02f4004079ae10edd019083b659
Read fragment views/localhost:3000//9c5de02f4004079ae10edd019083b659 (0.1ms)
  Rendered site/app/views/home/_product_group.html.erb (526.2ms)

这些都是缓存的,所以它应该只是直接从内存中放入 HTML 片段……事实上,相同的 HTML 片段一次又一次……但一个都会导致所有这些日志记录活动。每一个都需要500ms!这里发生了什么?

谢谢!

4

1 回答 1

1

您的记录器可能会成为瓶颈,尤其是对于 STDOUT。如果您的服务器将日志信息转储到您的终端,则可能是罪魁祸首。(是的,我知道这听起来很疯狂)

尝试在关闭日志记录或写入文件而不是 STDOUT 的情况下运行它。您可以异步地tail 或less -F 文件。

于 2020-09-24T17:08:03.183 回答