Rails Cache Sweeper - PullRequest
       3

Rails Cache Sweeper

4 голосов
/ 22 сентября 2010

Я пытаюсь реализовать Cache Sweeper, который бы фильтровал конкретное действие контроллера.

class ProductsController < ActionController  
    caches_action :index  
    cache_sweeper :product_sweeper  

    def index 
        @products = Product.all 
    end 

    def update_some_state
      #... do some stuff which doesn't trigger a product save, but invalidates cache
    end
end 

Класс подметальной машины:

class ProductSweeper < ActionController::Caching::Sweeper
    observe Product

    #expire fragment after model update
    def after_save
       expire_fragment('all_available_products')   
    end

    #expire different cache after controller method modifying state is called.
    def after_update_some_state
        expire_action(:controller => 'products', :action => 'index') 
    end
end

Обратный вызов ActiveRecord 'after_save' будет работать нормально, но обратный вызов для действия контроллера 'after_update_some_state' никогда не вызывается.

Ответы [ 2 ]

4 голосов
/ 23 сентября 2010

Похоже, я просто пропустил имя контроллера при попытке заставить работать обратные вызовы для действий контроллера. Мой Sweeper должен быть:

class ProductSweeper < ActionController::Caching::Sweeper
    observe Product

    #expire fragment after model update
    def after_save
       expire_fragment('all_available_products')   
    end

    #expire different cache after controller method modifying state is called.
    def after_products_update_some_state
        expire_action(:controller => 'products', :action => 'index') 
    end

    #can also use before:
    def before_products_update_some_state
        #do something before. 
    end
end
3 голосов
/ 22 сентября 2010

Я думаю, что ваша подметальная машина должна выглядеть так:

class ProductSweeper < ActionController::Caching::Sweeper
  observe Product

  def after_save(product)
     expire_cache(product)
  end

  def after_destroy(product)
    expire_cache(product)
  end

  private

  def expire_cache(product)
    expire_fragment('all_available_products')
    expire_page(:controller => 'products', :action => 'index')
  end 

after_index не является обратным вызовом, если вы его не определите.

В контроллере вы должны указать те действия, в которых должен запускаться уборщик, в спокойном виде эти действия должны быть create, update, destroy, поэтому объявление вашего контроллера должно выглядеть так:

class ProductsController < ActionController  
  caches_action :index  
  cache_sweeper :product_sweeper, :only => [:create, :update, :destroy]  

  def index 
    @products = Product.all 
  end 

  def create
    @product = Product.new(params[:product])

     if @product.save # triggers the sweeper.
       # do something
     else
       # do something else
     end
   end

  # update and stuff ...
end 

Надеюсь, это вам поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...