Mongoid Identity_map 对于 Web 请求打开,但对于后台或 rake 任务关闭?

发布于 2025-01-08 02:58:53 字数 98 浏览 5 评论 0原文

有没有办法为 Web 请求打开 Identity_map,但在控制台或 rake 任务中默认关闭?这似乎一直让我绊倒。

这是针对 mongoid 和 Rails 的。

Is there a way to have identity_map on for web requests, but off by default in the console or in rake tasks? This seems to keep tripping me up.

This is for mongoid and rails.

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

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

发布评论

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

评论(1

守不住的情 2025-01-15 02:58:53

如果您升级到 mongoid 3.0.0.rc,则可以重写 using_identity_map 方法来提供您想要的内容。
买者自负。

config/initializers/identity_map.rb

module Mongoid
  module UnitOfWork
    def using_identity_map?
      Mongoid.identity_map_enabled? && Threaded.identity_map_enabled? && (! defined? IRB) && ($0 !~ %r{rake})
    end
  end
end

Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.3'
gem 'jquery-rails'
gem 'builder', '3.0.0'
gem 'mongoid', '3.0.0.rc'
gem 'bson_ext'

请注意,3.0.0 的配置文件有显着不同,
并且默认模板当前仅设置开发
并且缺少测试和生产部分。

using_identity_map方法在3.0.0之前不可用,例如2.4.10,
您必须通过 .irbrc 或 Rakefile 显式重置 identity_map_enabled

If you move up to mongoid 3.0.0.rc, then the method using_identity_map can be overridden to provide what you want.
Caveat emptor.

config/initializers/identity_map.rb

module Mongoid
  module UnitOfWork
    def using_identity_map?
      Mongoid.identity_map_enabled? && Threaded.identity_map_enabled? && (! defined? IRB) && ($0 !~ %r{rake})
    end
  end
end

Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.3'
gem 'jquery-rails'
gem 'builder', '3.0.0'
gem 'mongoid', '3.0.0.rc'
gem 'bson_ext'

Note that the config file for 3.0.0 is significantly different,
and that the default template currently sets up development only
and is missing sections for test and production.

The using_identity_map method is not available prior to 3.0.0, e.g., 2.4.10,
where you will have to reset identity_map_enabled explicitly via .irbrc or Rakefile.

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