سؤال

I am not using the cache digest that rails4 suggest because I have this very infrequently change page that is updated by a rake task trigger by cronjob. I would like the rake task to invalidate an action cache

I am using mem_cache_store as my cache store.

The cached action can be request via:

http://localhost:3000/api/v1/shows/newest

This is my sweeper

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

This is my test_show_update rake task.

task :test_show_update => :environment do
  Show.first.save
end

When I run rake test_show_update, this is what I get:

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'

I could do Rails.cache.delete in my sweeper, but I don't even know what the cache key is

هل كانت مفيدة؟

المحلول

Turns out I cannot call expire action in rake, it can only be called in a controller. I installed memcached instead of relying on the default mem_store. Then I was able to peak at the action cache key. It is formated as view///

so I just have to do Rails.cache.delete on that cache in my sweeper.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top