Sinatra/Rails:在应用程序生命周期内保留自定义类实例
我可以断言 Rails/sinatra 应用程序仅初始化一次并且所有请求共享同一个应用程序吗 实例?或者新请求是否会产生新的应用程序实例?
是否可以实例化自定义类并在应用程序生命周期内保留它们而不使用会话、数据库存储或第三方服务?如果是这样,从线程安全的角度来看有何影响?
我正在尝试弄清楚如何实现基于网络的下载管理器,并且我目前正在评估基于 ruby 的框架。
Can I assert rails/sinatra apps are initialized only once and all requests share the same app
instance? or do new requests spawn new app instances?
Is it possible to instance custom classes and persist them during app lifetime without using sessions, database storages or third party services? If so, what are the implications from a thread-safeness point of view?
I'm trying to figure how to implement a web-based download manager and I'm currently evaluating ruby-based frameworks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般情况下不会。这实际上取决于您的设置。如果您仅在具有重用相同应用程序对象的服务器(又名机架处理程序)的进程上运行,则这将起作用。是否使用应用程序的同一实例取决于您使用的 Web 框架。例如,如果用作 Rack 端点,Sinatra 会为每个请求创建应用程序类的新实例(通常是
Sinatra::Application
)。如果你真的想坚持上课,你可能需要像磁悬浮这样的东西。但是,我相信您的意思是自定义类的持久实例。如果您不想使用数据库来实现持久性(您确实应该这样做),您可以使用 Ruby 附带的 PStore。
Not generally. This really depends on your setup. If you only run on process with a server (aka Rack handler) that reuses the same application object, this will work. Whether the same instance of your application is used depends on the web framework you are using. Sinatra for instance creates a new instance of your application class (usually
Sinatra::Application
) for every request if used as Rack endpoint.If you really want to persist classes, you will probably need something like maglev. However, I believe you mean persisting instances of custom classes. If you don't want to use a database for persistence (you really should), you could fall back to PStore, which ships with Ruby.