我正在开发一个Rails应用程序,我正在使用页面缓存来存储静态html输出.缓存工作正常.但是,我无法使缓存过期.
我相信我的问题部分是因为我没有从控制器到缓存.所有必要的操作都在模型中处理.这看起来应该是可行的,但我发现的所有对基于模型的缓存过期的引用似乎已经过时,或者在其他方面都不起作用.
在我的environment.rb文件中,我正在调用
config.load_paths += %W( #{RAILS_ROOT}/app/sweepers )
Run Code Online (Sandbox Code Playgroud)
我在/ sweepers文件夹中有一个LinkSweeper文件:
class LinkSweeper < ActionController::Caching::Sweeper
observe Link
def after_update(link)
clear_links_cache(link)
end
def clear_links_cache(link)
# expire_page :controller => 'links', :action => 'show', :md5 => link.md5
expire_page '/l/'+ link.md5 + '.html'
end
end
Run Code Online (Sandbox Code Playgroud)
那么...... 为什么在更新模型时不删除缓存页面?(进程:使用脚本/控制台,我从数据库中选择项目并保存它们,但是它们的相应页面没有从缓存中删除),我也在调用通常会调用的链接模型中的特定方法扫地机.两者都不起作用.
如果重要,则缓存文件是Links表中键值的md5哈希值.缓存页面存储为/l/45ed4aade64d427...99919cba2bd90f.html.
从本质上讲,似乎扫地机实际上并没有观察到链路.我还读到(这里)可能只是简单地将sweeper添加到environment.rb中的config.active_record.observers,但这似乎没有做到(我不确定app/sweepers的load_path)在environment.rb中避免了).
我有一个扫地机应该到期一些动作缓存.即使调试器在调用expire_action之前立即停止,它实际上并没有使操作到期.知道会发生什么吗?
这是相关的清扫车和控制器.
#company_sweeper.rb(在'models'目录中)
class CompanySweeper < ActionController::Caching::Sweeper
observe Company
def after_save(company)
expire_cache(company) if company.final_save && company.valid?
end
def expire_cache(company)
debugger <= #debugger stops here!
right before the call
I'm trying to make.
expire_action :controller => 'reports',
:action => 'full_report'
end
end
Run Code Online (Sandbox Code Playgroud)
#reports_controller.rb
class ReportsController < ApplicationController
layout false
caches_action :full_report, :supplier_list, :service_categories
cache_sweeper :company_sweeper
def full_report
#do stuff...
end
end
Run Code Online (Sandbox Code Playgroud)
我知道它没有到期的方式是完整的报告返回旧数据,并几乎立即响应.很奇怪,对吗?
我有动作缓存工作在我的网站索引,并设置一个工作正常的SiteSweeper:
# app/controllers/admin/sites_controller.rb
class Admin::SitesController < Admin::BaseController
cache_sweeper :site_sweeper, :only => [:create, :update, :destroy]
caches_action :index, :cache_path => '/admin/sites'
...
# app/sweepers/site_sweeper.rb
class SiteSweeper < ActionController::Caching::Sweeper
observe Site
def after_save(site)
expire_cache(site)
end
def after_destroy(site)
expire_cache(site)
end
def expire_cache(site)
expire_action '/admin/sites'
end
end
Run Code Online (Sandbox Code Playgroud)
但是,无论何时保存或销毁任何发布者,我也希望过期/管理/站点.是否有可能让PublisherSweeper使用这样的东西使网站索引到期?
# app/sweepers/publisher_sweeper.rb
class PublisherSweeper < ActionController::Caching::Sweeper
observe Publisher
def after_save(publisher)
expire_cache(publisher)
end
def after_destroy(publisher)
expire_cache(publisher)
end
def expire_cache(publisher)
expire_action '/admin/sites'
end
end
Run Code Online (Sandbox Code Playgroud)
我知道我可以在各种Publisher动作中调用expire_action'/ admin/sites'.我只是想知道扫地机是否具备此功能(以保持我的控制器更清洁).