우리 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_delete의 after_update를 추출, 그리고라는 모듈 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는 스위퍼 안에서 메서드를 명시 적으로 정의 할 때만 작동합니다. 모듈에 콜백 메소드를 추출하는 쉬운 방법이 있습니까?
매우 이상합니다. 두 가지 예는 모두 나를 위해 일합니다. 캐시 저장소에 무엇을 사용하고 있습니까? –
현재 코드에서 작동해야합니다. 아무런 변경이 필요 없습니다. include 문이 모든 것을 처리합니다. – Anand
모듈 기반 메소드 after_create 등이 전혀 호출되지 않습니까? – moritz