3

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?

ace
  • 865
  • 2
  • 15
  • 24
  • 1
    http is stateless protocol, php or not. sessions are a common way of adding a state, but im not sure what you really asking –  Feb 20 '11 at 04:45
  • The answers have helped me find the information I was looking for. In particular [this article on java stateful web apps](http://www.ibm.com/developerworks/library/j-jtp09238.html) covered it exactly. [Web sockets](http://www.html5rocks.com/tutorials/websockets/basics/) appear to address it. [Albatross](http://wiki.python.org/moin/Albatross) for Python is another framework for stateful web apps. – ace Feb 20 '11 at 15:17
  • 1
    The difference with Java, Python, and Ruby is that the applications themselves have state; they don't tear down after each request like PHP. If you think web sockets are a good move, Node.js might really be the golden ticket you're looking for. – coreyward Feb 20 '11 at 15:57
  • @coreyward thanks for all the info, that was exactly what I wanted to know. Just for the sake of rounding it out a bit more [here](http://docs.djangoproject.com/en/dev/topics/http/sessions/#topics-http-sessions) it discusses how Django maintains state using the database or memcached just like PHP would. – ace Feb 20 '11 at 17:01

2 Answers2

4

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.

coreyward
  • 77,547
  • 20
  • 137
  • 166
  • It's a game in which the user updates the placement of objects in the browser and can interact with other players real time. For the real time inter-user communication I planned on using PHP CLI and sockets, the problem was having to reload the model each time the user updated a browser component. – ace Feb 20 '11 at 15:23
  • 1
    @ace You definitely don't want your users having direct access to PHP CLI. This sounds like a good job for Node.js, but that means bringing on an additional dependency. There are other methods like long-polling AJAX requests (Comet) that can help, but regardless this is a hard problem to solve with PHP due to limited resources restricting the number of concurrent requests. – coreyward Feb 20 '11 at 15:55
  • Check this https://www.swoole.co.uk/ - PHP with Async IO – Elizandro - SparcBR May 01 '21 at 18:32
1

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

pprain
  • 83
  • 7