我可以在 Rails 3 中以编程方式更改 config.cache_classes 吗?

发布于 2024-11-28 12:09:28 字数 179 浏览 1 评论 0原文

我有一些针对我的开发轨道服务器运行的 iPhone 客户端测试。如果我在 Rails 配置中打开类缓存,整个套件的运行速度会快一个数量级。另一方面,当我没有实际运行测试时,这会减慢开发速度。

我希望测试套件在开始时执行一个操作以打开类缓存,并在最后执行另一个操作以再次关闭类缓存。

这可能吗?如果是这样,怎么办?

I have some iPhone client tests that run against my development rails server. The whole suite runs an order of magnitude faster if I turn on class caching in the Rails config. On the other hand, that slows down development when I'm not actually running the tests.

I want the test suite to hit an action at the beginning to turn on class caching and another action at the end to turn class caching off again.

Is this even possible? If so, how?

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

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

发布评论

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

评论(2

少女的英雄梦 2024-12-05 12:09:28

并非没有一些严重的黑客攻击。 Rails 花费了很多精力来确保每次请求时都重新加载文件(当 cache_classes=false 时)。初始化程序在多个地方使用 cache_classes 配置变量的值,其中最重要的是:

  • 当 cache_classes 为 truerequire 加载 ruby​​ 文件code> (意味着它们不再可重新加载)
  • 设置调度程序回调,以便在 cache_classesfalse 时在每个请求上重新加载应用程序

您确实可以访问cache_classes 变量,您甚至可以根据需要更改它:

Rails.configuration.cache_classes = true

但是,这不会对正在运行的 Rails 实例产生任何影响,因为使用该值的初始值设定项仅在 Rails 应用程序启动时运行一次。

这意味着,除非您准备投入一些认真的时间和黑客努力,否则您无法真正避免服务器的重新启动。因此,您需要注意的是通过测试套件控制此重新启动过程。

例如,您可以尝试从rails内部重新启动rails。这将允许您定义一个操作,您的测试套件可以在开始执行之前执行(以正确的模式重新启动服务器),以及服务器可以在所有测试完成后执行的另一个操作,以使用 cache_classes 设置为原来的值。您可以通过环境变量 控制缓存类的值就像这篇文章所建议的

设置所有这些并将其组合在一起仍然需要一些工作,但如果您想要一个“自动神奇”解决方案,这可能是您最好的选择。

Not without some serious hacking. Rails goes to quite a lot of trouble to make sure your files are reloaded on every request (when cache_classes=false). The value of the cache_classes configuration variable is used by initializers in several places not the least of which being:

  • using require to load ruby files when cache_classes is true (meaning they are no longer reloadable)
  • setting up dispatcher callbacks to reaload the application on every request when cache_classes is false

You do have access to the value of the cache_classes variable, and you can even change it if you like:

Rails.configuration.cache_classes = true

But, this will have no effect on the running rails instance as the initializers where that value is used only run once when the rails app starts up.

What this means is this, unless you're prepared to invest some serious time and hacking effort you can't really avoid a restart of your server. So, what you need to look at is controlling this restart process via your test suite.

For example you can try to restart rails from within rails. This would allow you to define an action that your test suite can hit right before it begins executing (to restart the server in the right mode), and another action which the server can hit after all tests have finished, to restart everything with cache_classes set to what it used to be. You would control the value of cache classes via an environment variable like this post suggests.

It would still require a bit of work to set all of this up and get it to hang together, but this is probably your best bet if you want an 'auto-magical' solution.

居里长安 2024-12-05 12:09:28

我不认为按照你的建议去做会起作用。

但我建议您可能正在寻找错误的解决方案。

如果您想要从 iPhone 测试中访问您的开发数据库,
那为什么不添加一个新的环境呢。

添加一个新文件 config/environments/iphone_dev.rb

require File.dirname(__FILE__)+"/development.rb"
config.cache_classes = true

并在您的database.yml(或 mongoid.yml 或其他)中

iphone_dev:
  host: localhost
  database: my_app_development

数据库没有理由不能相同

现在只需运行 rails server -eiphone_dev -p3001

你应该有一个服务器,几乎与你的开发服务器相同,
但在不同的端口上运行,并启用缓存。

I don't think doing what you suggest will work.

But I suggest you may be looking for the wrong solution.

If what you want is to access your development database from your iphone testing,
then why not add a new environment.

Add a new file config/environments/iphone_dev.rb

require File.dirname(__FILE__)+"/development.rb"
config.cache_classes = true

And in your database.yml (or mongoid.yml or whatever)

iphone_dev:
  host: localhost
  database: my_app_development

There is no reason the database cant be the same

Now just run rails server -eiphone_dev -p3001

You should have a server, almost the same as your dev server,
but running on a different port, with caching enabled.

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