0

I've a CategoryController action which responds with a complex Category tree (counts containing Products. For better performance I use Memcached. It works: First request 200ms, second request 12ms.

What I try to achieve:

When a product is added to a Category, it should invalidate the Category action "main_menu".

This is what I've so far, but the CategorySweeper methods are never called:


class CategoryController < ApplicationController

  caches_action :main_menu, expires_in: 1.day

  cache_sweeper CategorySweeper, only: [:main_menu]

  def main_menu
    render json: Category.navigation
  end
end

class CategorySweeper < ActionController::Caching::Sweeper
  observe Category, Product 

  def after_create(category)
    puts "##### update #{category}" # never gets called
    expire_cache_for(category)
  end

  def after_update(category)
    puts "##### update #{category}" # never gets called
    expire_cache_for(category)
  end

  private

  def expire_cache_for(category)
    expire_page(controller: 'category', action: 'main_menu')
    expire_action(controller: "category", action: 'main_menu')
  end
end

of course I've the gem in my Gemfile

gem 'actionpack-action_caching', github: 'rails/actionpack-action_caching', :branch => 'master'
gem 'rails-observers'

What I am doing wrong? Am I using the gem incorrect?

Thank you in advance

Jan
  • 12,992
  • 9
  • 53
  • 89

0 Answers0