我没有使用rails4建议的缓存摘要,因为我有这个非常不常见的更改页面,由cronjob的rake任务触发器更新。我希望rake任务使动作缓存无效
我使用mem_cache_store作为我的缓存存储。
可以通过以下方式请求缓存操作:
http://localhost:3000/api/v1/shows/newest
这是我的清扫工
class ShowSweeper < ActionController::Caching::Sweeper
observe Show
def after_save(show)
expire_cache(show)
end
def after_destroy(show)
expire_cache(show)
end
def expire_cache(show)
expire_action(controller:"api/v1/shows", action:"newest", format:"json")
end
end
这是我的test_show_update rake任务。
task :test_show_update => :environment do
Show.first.save
end
当我运行rake test_show_update时,这就是我得到的:
rake aborted!
undefined method `expire_action' for #<ShowSweeper:0x00000101482bd0 @controller=nil>
/Users/etse/.rvm/gems/ruby-2.1.0/gems/rails-observers-0.1.2/lib/rails/observers/action_controller/caching/sweeping.rb:105:in `method_missing'
/Users/etse/Documents/workspace/Rails/PodcastPlusServer/app/sweepers/show_sweeper.rb:17:in `expire_cache'
/Users/etse/Documents/workspace/Rails/PodcastPlusServer/app/sweepers/show_sweeper.rb:5:in `after_save'
/Users/etse/.rvm/gems/ruby-2.1.0/gems/rails-observers-0.1.2/lib/rails/observers/active_model/observing.rb:352:in `update'
/Users/etse/.rvm/gems/ruby-2.1.0/gems/rails-observers-0.1.2/lib/rails/observers/activerecord/observer.rb:118:in `block (2 levels) in define_callbacks'
/Users/etse/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:397:in `_run__404559172311641424__save__callbacks'
/Users/etse/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
我可以在清扫器中执行Rails.cache.delete,但我甚至不知道缓存键是什么
答案 0 :(得分:1)
原来我不能在rake中调用expire动作,它只能在控制器中调用。我安装了memcached而不是依赖于默认的mem_store。然后我能够在动作缓存键达到峰值。它被格式化为 视图///
所以我只需要在清扫器中对该缓存执行Rails.cache.delete。