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

我误解了,或者我试图在一个广场上贴合圈子吗?MEMCACHED似乎是将模型保持在页面请求之间的内存中的一个很好的解决方案,但它仍然需要加载缓存。PHP CLI似乎有前途,但在调查它之后,它似乎比价值更麻烦。任何建议?

有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top