Sinatra/Rails:在应用程序生命周期内保留自定义类实例

发布于 2024-08-29 23:30:57 字数 207 浏览 1 评论 0原文

我可以断言 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 技术交流群。

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

发布评论

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

评论(1

ゃ人海孤独症 2024-09-05 23:30:57

我可以断言 Rails/sinatra 应用程序是
仅初始化一次且所有请求
共享同一个应用程序实例?或者做新的
请求生成新的应用程序实例?

一般情况下不会。这实际上取决于您的设置。如果您仅在具有重用相同应用程序对象的服务器(又名机架处理程序)的进程上运行,则这将起作用。是否使用应用程序的同一实例取决于您使用的 Web 框架。例如,如果用作 Rack 端点,Sinatra 会为每个请求创建应用程序类的新实例(通常是 Sinatra::Application)。

是否可以自定义实例
类并在应用程序期间保留它们
不使用会话的生命周期,
数据库存储或第三方
服务?如果有的话,有哪些
线程安全的影响
观点?

如果你真的想坚持上课,你可能需要像磁悬浮这样的东西。但是,我相信您的意思是自定义类的持久实例。如果您不想使用数据库来实现持久性(您确实应该这样做),您可以使用 Ruby 附带的 PStore。

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?

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.

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?

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.

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