Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP pooling functionality (not just database connections)

Is it possible to pool data or functionality in PHP?

The amateur-ish PHP code that I write wakes up to handle a response, loads functions, opens database connections, create objects, initialises them, and then - dies after 0.01 secs when the response has been processed, leaving the next request to reload, parse, and run mainly the same thing again.

That's non-sensical, and I find it removes the value of a lot of my work not to have functionality/data/object pooling. For example I can write classes, to find they all get reinitialised with each request - what's the point of me trying to develop a meaningful object structure?

And so: how can I write PHP to pool data and functionality?

like image 269
boisvert Avatar asked Feb 23 '23 22:02

boisvert


2 Answers

There is no 1 solution pooling or persistent state in PHP, it hasn't got an application state like Java, it more or less follows the stateless protocol that is HTTP. What you can do is:

  • Create persistent connections to databases (i.e. they will be reused if you call them with the same parameters, they won't magically exist but you can avoid the overhead of an actual connect).
  • Store objects in sessions to keep a calculated state (they will be serialized, and unserialized on the next request).
  • Route work that requires significant, but one-time, initialization to a daemon running independent from the webserver (a gearman server & workers come to mind).
  • But in the end, if you application needs a global state, maybe PHP just isn't the right solution.
like image 145
Wrikken Avatar answered Mar 06 '23 14:03

Wrikken


PHP is hardly ever the bottleneck. Our servers handle hundreds of requests per second at peak moments. And those are not tiny requests too. It seems illogical, but PHP is actually very fast. And you can use APC cache to cache precompiled PHP files to make it even faster. Then, you can use MemCache to store data, so any query results and data like that can be cached easily without relying on the suboptimal query cache of MySQL.

like image 36
GolezTrol Avatar answered Mar 06 '23 15:03

GolezTrol