0

我正在使用片段缓存来加快 Comfortable Mexican Sofa 的渲染时间。但是,当我更新它时,我无法弄清楚如何让它使特定对象的缓存过期。

我正在使用 Comfy 作为我正在构建的公司网站的 CMS。为了允许动态页面内容,我对其进行了设置,以便将页面目录呈现为内容块。

class WelcomeController < ApplicationController
  def index
    @testimonials = Comfy::Cms::Page.find_by_full_path!("/testimonials").children
    @clients = Comfy::Cms::Page.find_by_full_path!("/clients").children
    @recent_blogs = Comfy::Cms::Page.find_by_full_path!("/blog").children.published.last(4)
    @team = Comfy::Cms::Page.find_by_full_path!("/team").children
  end

end

然后,我使用cms_block_contentCMS 提供的帮助器来渲染集合。

<% @clients.each do | client |%>
    <img class="client__logo lazy" data-original="<%=cms_block_content(:client_logo, client).file.url%>">
<%end%>

我还介绍了一些片段缓存,因为所有内联渲染都大大减慢了页面的加载速度。

但是,我遇到了一个问题。当我创建或删除新内容时,它会很好地出现/消失在页面上,但是,当我更新内容时,页面上的内容不会更新。更新内容似乎不会使缓存的内容过期(如果您运行,Rails.cache.clear则会加载更新的内容)。

我研究了创建CMS 文档中提出的缓存清扫器,但我不太确定如何进行,因为我不确定将哪些参数传递给实际expire_fragment方法。

class CmsAdminSweeper < ActionController::Caching::Sweeper
  observe Comfy::Cms::Page

  def after_update(record)
    do_sweeping(record)
  end

  def do_sweeping(record)
    # return unless modification is made from controller action
    return false if session.blank? || assigns(:site).blank?

    Rails.logger.info("CmsAdminSweeper.do_sweeping in progress...")

    expire_fragment({ controller: '/welcome', action: 'index', id: record.id})
  end
end

这是最好的方法吗?如果是这样,我可以将什么传递给 expire_fragment 方法?

非常感谢!

汤姆

4

1 回答 1

1

正确的答案其实一直盯着我的脸,我只是没有意识到。我需要做的就是通过记录。

expire_fragment(record)

但是,当我第一次尝试它时它不起作用的原因是因为摘要在保存时被添加到缓存中。这意味着您不能手动使它们过期。因此,当您缓存视图时,您需要确保您正在跳过摘要。

<% @clients.each do | client |%>
     <% cache client, skip_digest: true do %>
          <img class="client__logo lazy" data-original="<%=cms_block_content(:client_logo, client).file.url%>">
     <%end%>
 <%end%>

瞧!有用。

于 2017-02-06T13:06:34.000 回答