简体   繁体   English

为什么Rails会为每个请求创建一个控制器?

[英]Why does Rails create a controller for every request?

From my previous question I understood that Rails creates a controller instance for every request. 从我之前的问题中我了解到Rails为每个请求创建一个控制器实例。

My question is, because this subject is related to the design of the project I'm working on: 我的问题是,因为这个主题与我正在研究的项目的设计有关:

Why does Rails creates a new instance of 为什么Rails会创建一个新的实例

class SomeController < ApplicationController; end

to process every incoming request? 处理每个传入的请求? Why not just create singleton object and forward requests to this one? 为什么不创建单例对象并将请求转发给这个? This seems more efficient as we won't waste resources on allocating and cleaning objects for request? 这似乎更有效,因为我们不会浪费资源来分配和清理请求对象?

The overhead of instantiating a new controller instance is insignificant, and it means there is no accidentally shared state between two completely unrelated requests. 实例化新控制器实例的开销是微不足道的,这意味着两个完全不相关的请求之间没有意外共享状态。 Any "savings" in processor time would be more than offset by the potential to produce devastating bugs. 处理器时间的任何“节省”都会被产生毁灭性错误的可能性所抵消。

Remember that controllers are for storing request-specific state. 请记住,控制器用于存储特定于请求的状态。 Reusing controllers would require you to reset every @variable you'd ever set, at the start of every action. 重复使用控制器将要求您在每个操作开始时重置您设置的每个@variable Otherwise, something like @is_admin = true could wind-up being set and never cleared. 否则,像@is_admin = true这样的东西可能会被设置并且永远不会被清除。 The less contrived bugs you'd actually be introducing would be much more subtle and draining on developer time. 你实际上引入的不那么人为的错误会更加微妙,并且会影响开发人员的时间。

You're seeing optimizations where there are none. 你看到没有的优化。 Something must maintain state and reset it between requests, or you have this nightmare of accidentally shared state. 某些东西必须保持状态并在请求之间重置它,否则你就会遇到意外共享状态的噩梦。 If you persist controller instances between requests, you're simply pushing the job of maintaining/resetting state down to some lower level, where the answer will likely still be to instantiate a fresh instance of some state-managing class for each request. 如果在请求之间保留控制器实例,则只需将维护/重置状态的工作推送到某个较低级别,其中答案可能仍然是为每个请求实例化某个状态管理类的新实例。 Computers are very good at allocating and freeing resources, so never worry about that until you actually know it's a bottleneck. 计算机非常擅长分配和释放资源,所以在你真正知道这是一个瓶颈之前不要担心。 In this case, instantiating a new controller for each request is easily the correct choice. 在这种情况下,为每个请求实例化一个新控制器很容易是正确的选择。

In the case of Rails, being able to use @variable = value is a major win from a code-clarity and usability stand-point, and this more or less necessitates discarding each instance of a controller when the request completes. 对于Rails,能够使用@variable = value是代码清晰度和可用性立场的主要胜利,这或多或少需要在请求完成时丢弃控制器的每个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails:为什么simple_token_authentication验证每个请求? - Rails: why does simple_token_authentication validate every request? 为什么Rails缓存会检查每个页面请求上的URL? - why does rails caching check the URL on every page request? 为什么Rails不会在每个请求上刷新类(尽管配置)? - Why does Rails not refresh classes on every request (despite configuration)? 为什么 Ruby on Rails 会在每次点击时(有时)创建新会话? - Why does Ruby on Rails create new Sessions on every hit (sometimes)? 为什么我的控制器没有在用户控制器的创建操作中设置属性? - Rails 3 - Why does my controller not set an attribute in the create action of my User's controller? - Rails 3 Rails:为什么我覆盖的Devise控制器上的RSpec崩溃要求规范? - Rails: Why does RSpec crash on my overridden Devise controller request spec? 为什么在Rails控制器中为CREATE而不是NEW设置强参数? - why strong parameters for the CREATE, but not NEW, in a rails controller? 为什么Rails生成控制器会为每个数据库项目在路由中创建这些Get? - Why does rails generate controller create these gets in routes for each database item? 减少控制器中每个“创建和更新”操作的功劳。 滑轨4 - Decrease credit for every Create and Update action in controller. Rails 4 为什么我的Rails控制器测试没有路由? - Why does my Rails controller test not route?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM