HTTP 请求之间具有持久状态的模型

发布于 2024-10-18 17:46:45 字数 304 浏览 0 评论 0原文

我想创建一个具有在 HTTP 请求之间持续存在的模型的 Web 应用程序。据我了解,像 PHP 这样的语言将每个 HTTP 请求都视为一个全新的连接,除了一些全局变量(如 SESSION);因此,每次用户更改页面时,我的所有 PHP 类都会再次加载到内存中(每个 AJAX 请求也会执行此操作) - 要求我每次都从数据库构建。

我是错了还是我试图让一个圆适合一个正方形? Memcached 似乎是一个很好的解决方案,可以在页面请求之间将模型保留在内存中,但它仍然需要加载缓存。 PHP CLI 看起来很有前途,但经过更多研究后,发现它带来的麻烦比它值得的还要多。有什么建议吗?

I want to create a web application with a model that persists between HTTP requests. From what I understand languages like PHP treat each HTTP request as a brand new connection except for some global variables like SESSION; so each time the user changes pages all my PHP classes are loaded into memory again (and each AJAX request does this too) - requiring me to build from the database each time.

Am I mistaken or am I trying to make a circle fit in a square? Memcached seems to be a good solution for keeping my model in memory between page requests but it still needs to load the cache. PHP CLI seemed promising but after looking into it more it seemed like it would be more trouble than it was worth. Any suggestions?

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

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

发布评论

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

评论(2

ぽ尐不点ル 2024-10-25 17:46:45

您应该避免在 Web 应用程序中要求持久状态; HTTP 是无状态的,您需要围绕它设计业务逻辑。此外,PHP 的内存泄漏问题也不是很安全,因为它不打算充当守护进程或长时间运行。您也不应该维护 PHP 变量中的信息数据库。您可以将昂贵的查询结果缓存在内存缓存中,并以极低的延迟检索它们。

您可以序列化模型,将其存储在会话(或内存缓存)中,并在下一个请求时反序列化,以便将相关变量保持在请求之间的范围内。如果您分享更多有关您的应用程序的具体信息,也许我们可以帮助您找出处理此问题的最佳方法。

You should avoid requiring a persistent state in your web application; HTTP is stateless and you need to design your business logic around that. Also, PHP isn't very memory-leak safe since it isn't meant to act as a daemon or run for extended periods. You shouldn't be maintaining a database of information in PHP variables either. You can cache expensive query results in memcache and retrieve them with very little latency.

You can serialize your model, store it in a session (or memcache), and deserialize it on the next request in order to keep relevant variables within scope between requests. If you share a bit more about the specifics of your application, perhaps we can help you figure out the best way to handle this.

梦毁影碎の 2024-10-25 17:46:45

我确实同意人们应该尽量避免在请求之间共享状态。但在极少数情况下,例如通过 HTTP 实现简单的消息队列,最好拥有此功能。

对于 php,可以通过 php_shmop 扩展使用 IPC。

对于nodejs,我找到了 this

I do agree that one should try to avoid sharing states between requests. But in rare cases, such as to implement a simple message queue over HTTP, It's desirable to have this feature in hand.

For php, one could use IPC via the php_shmop extension.

For nodejs, I found this

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