我們在Rails應用程序中以下機:如何在清掃器中包含緩存到期的模塊?
class AgencyEquipmentTypeSweeper < ActionController::Caching::Sweeper
observe AgencyEquipmentType
#include ExpireOptions
def after_update(agency_equipment_type)
expire_options(agency_equipment_type)
end
def after_delete(agency_equipment_type)
expire_options(agency_equipment_type)
end
def after_create(agency_equipment_type)
expire_options(agency_equipment_type)
end
def expire_options(agency_equipment_type)
Rails.cache.delete("agency_equipment_type_options/#{agency_equipment_type.agency_id}")
end
end
我們想抽取after_update,after_delete和after_create回調到一個名爲模塊「ExpireOptions」
模塊應該外觀像這樣(與「expire_options」的方法在 原來清掃留守):
module ExpireOptions
def after_update(record)
expire_options(record)
end
def after_delete(record)
expire_options(record)
end
def after_create(record)
expire_options(record)
end
end
class AgencyEquipmentTypeSweeper < ActionController::Caching::Sweeper
observe AgencyEquipmentType
include ExpireOptions
def expire_options(agency_equipment_type)
Rails.cache.delete("agency_equipment_type_options/#{agency_equipment_type.agency_id}")
end
end
但緩存expirati如果我們在清理程序中明確定義方法,ons纔會起作用。有沒有簡單的方法來提取這些回調方法到一個模塊,並仍然有它們的工作?
這很奇怪。這兩個例子都適用於本地。你用什麼來做你的緩存存儲? – 2011-05-25 07:08:16
它應該與您當前的代碼一起工作。無需更改。包括聲明照顧一切。 – Anand 2011-05-27 10:12:20
基於模塊的方法after_create等被調用嗎? – moritz 2011-05-31 17:56:46