我可以在 Rails 3 中以编程方式更改 config.cache_classes 吗?
我有一些针对我的开发轨道服务器运行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并非没有一些严重的黑客攻击。 Rails 花费了很多精力来确保每次请求时都重新加载文件(当
cache_classes=false
时)。初始化程序在多个地方使用cache_classes
配置变量的值,其中最重要的是:truerequire
加载 ruby 文件code> (意味着它们不再可重新加载)cache_classes
为false
时在每个请求上重新加载应用程序您确实可以访问
cache_classes
变量,您甚至可以根据需要更改它:但是,这不会对正在运行的 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 thecache_classes
configuration variable is used by initializers in several places not the least of which being:require
to load ruby files when cache_classes istrue
(meaning they are no longer reloadable)cache_classes
isfalse
You do have access to the value of the
cache_classes
variable, and you can even change it if you like: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.
我不认为按照你的建议去做会起作用。
但我建议您可能正在寻找错误的解决方案。
如果您想要从 iPhone 测试中访问您的开发数据库,
那为什么不添加一个新的环境呢。
添加一个新文件 config/environments/iphone_dev.rb
并在您的database.yml(或 mongoid.yml 或其他)中
数据库没有理由不能相同
现在只需运行
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
And in your database.yml (or mongoid.yml or whatever)
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.