我正在使用片段缓存来加快 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_content
CMS 提供的帮助器来渲染集合。
<% @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 方法?
非常感谢!
汤姆