views:

310

answers:

6

I've been writing PHP for years, and have used every framework under the sun, but one thing has always bugged me... and that's that the whole bloody thing has to be interpreted and executed every time someone tells my server they want the page served.

I've experimented with caching, FastCGI, the Zend Job Queue (and symfony plug-ins that do similar - as well as my own DB-based solutions that implement the System_Daemon class to run background processes) and I've managed to make my apps fairly quick using all that stuff... but I can't get over the mental block that my settings files, system/environment check functions, and all the stuff that should only really be loaded ONCE... loads every darn time someone hits my page.

So, my ramble leads to the following Q--

Is there some method/technique for loading certain aspects of PHP into RAM so that when that page is requested, all my settings.yml files, system checks, framework files, cached pages etc can be loaded directly from memory without ever even touching the HD... or needing to go through the same loading mechanism 50,000 times per day to init the program?

If there's nothing in PHP... are there any other 'web' languages that can be compiled in this way, to allow for true init-once apps?

+7  A: 

I think you should give memcached a try, if you're talking about caching data. I think PHP is fairly proficient in caching compiled php-pages if you use stuff like mod_php in apache (which doesn't die in between requests).

roe
+1  A: 

Create a full page cache on the ram disk and make your web server serve the page from there. This is a method that wordpress supercache plugin uses and it works great if your web site is suitable for full page caching. This whay you are not even invoking the PHP interpreter.

For users that are logged in (have an open session) you can create a rewrite condition that will redirect their request to the PHP engine.

Also, always use an opcode cache like APC and use it for caching config files (memcache is also fine).

Goran Jurić
+1  A: 

If you are asking for a JVM/Tomcat like application server, then the answer is likely no. To my knowledge nothing (usable) like this exists for PHP. PHP uses a shared-nothing architecture, so it is by design everything is setup on all requests. But actually, this makes PHP scale pretty well.

As for speeding up your apps, try to use memcached and a code accelerator. Maybe look into Zend Server to get a complete package.

Gordon
A: 

Regarding your last question, I believe at least most of the Python and Ruby web frameworks work like that.

Ruby web applications are nowadays built so that the app is only initialized once per server process. When requests come in, the server (Apache, for example) passes them to the web application (over Rack interface) which is running on the background.

This is how web frameworks based on Rack work. Older versions of Ruby on Rails were similar, although they used a different interface to talk to the web server.

hrnt
Is there an equivalent for PHP?
Lee
+5  A: 

Take a look on APC (Alternative PHP Cache), it keeps a cache of compiled files (PHP Opcode) and also lets you store random variables on memory with apc_fetch, apc_store.

The instalation is very simple and it really gives a boost on performance.

Felipe Ribeiro
+1 because apc is more than an opcode cache
gpilotino
Also this is going to be built into php 6
Mike Valstar
A: 

I'd keep an eye on the Facebook Engineering ppage (http://www.facebook.com/notes.php?id=9445547199), every now and then they come up with posts about how they keep things fast/optimize/scale. I think they're use of php is super impressive.

sjobe