HTTP请求之间具有持久状态的模型 [英] Models with persistent state between HTTP requests

查看:82
本文介绍了HTTP请求之间具有持久状态的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Web应用程序,其模型在HTTP请求之间保持不变.据我了解,PHP之类的语言将每个HTTP请求视为一个全新的连接,除了诸如SESSION之类的一些全局变量之外.因此,每次用户更改页面时,我所有的PHP类都再次加载到内存中(每个AJAX请求也将其加载到内存中)-每次都需要我从数据库进行构建.

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.

是我误会还是想使圆适合正方形? Memcached似乎是在页面请求之间将我的模型保存在内存中的一个很好的解决方案,但是它仍然需要加载缓存. PHP CLI似乎很有前途,但在对其进行更多研究之后,似乎麻烦多于其应有的价值.有什么建议吗?

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应用程序中要求保持持久状态; 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.

这篇关于HTTP请求之间具有持久状态的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆