如何在清理器中包含具有缓存过期的模块?

发布于 2024-10-25 04:26:02 字数 1330 浏览 1 评论 0原文

我们在 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

但是只有当我们在清理器内部显式定义方法时,缓存过期才起作用。有没有一种简单的方法可以将这些回调方法提取到模块中,并且仍然让它们工作?

We have the following sweeper in a rails application:

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

We'd like to extract the after_update, after_delete, and after_create callbacks to a module called "ExpireOptions"

The module should look like this (with the 'expire_options' method staying behind in the
original sweeper):

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

BUT the cache expirations only work if we define the methods explicitly inside the sweeper. Is there an easy way to extract those callback methods to a module, and still have them work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你又不是我 2024-11-01 04:26:02

尝试使用:

module ExpireOptions
  def self.included(base)
    base.class_eval do
      after_update :custom_after_update
      after_delete :custom_after_delete
      after_create :custom_after_create
    end
  end

  def custom_after_update(record)
    expire_options(record)
  end

  def custom_after_delete(record)
    expire_options(record)
  end

  def custom_after_create(record)
    expire_options(record)
  end
end

Try with:

module ExpireOptions
  def self.included(base)
    base.class_eval do
      after_update :custom_after_update
      after_delete :custom_after_delete
      after_create :custom_after_create
    end
  end

  def custom_after_update(record)
    expire_options(record)
  end

  def custom_after_delete(record)
    expire_options(record)
  end

  def custom_after_create(record)
    expire_options(record)
  end
end
凉薄对峙 2024-11-01 04:26:02

我会尝试类似的方法:

module ExpireOptions
  def after_update(record)
    self.send(:expire_options, record)
  end

  def after_delete(record)
    self.send(:expire_options, record)
  end

  def after_create(record)
    self.send(:expire_options, record)
  end
end

这应该确保它不会尝试在模块上调用这些方法,而是在 self 上调用这些方法,这有望成为调用对象。

这有帮助吗?

I would try something like:

module ExpireOptions
  def after_update(record)
    self.send(:expire_options, record)
  end

  def after_delete(record)
    self.send(:expire_options, record)
  end

  def after_create(record)
    self.send(:expire_options, record)
  end
end

This should make sure it does not try to call those methods on the module, but on self which would hopefully be the calling object.

Does that help?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文