全部,
我正在尝试向我的 Rails 3.1 应用程序添加缓存。清扫器位于默认命名空间中,而我的控制器位于 Admin 命名空间中。
例如,我在 Admin 命名空间中有 BooksController,每当这个控制器中的 share 方法我希望图书缓存被清除时。我尝试将此方法命名为 after_books_share,但未调用该方法。
class Admin::BooksController < ApplicationController
caches_action :show
cache_sweeper :book_sweeper
def share
# "Share" a book
end
end
class BookSweeper < ActionController::Caching::Sweeper
observe Book
def after_update(book)
expire_cache_for(book)
end
def after_books_share
book = Book.find params[:id]
expire_cache_for(book)
end
def expire_cache_for(book)
expire_action(:controller => '/books', :action => 'show', :id => book)
end
end