views:

277

answers:

4

I was wondering about caching dynamic PHP pages. Is it really about pre-compiling the PHP code and storing it in byte-code? Something similar to Python's .pyc which is a more compiled and ready to execute version and so that if the system sees that the .pyc file is newer than the .py file, then it won't bother to re-compile to .py file.

So is PHP caching mainly about this? Can someone offer a little bit more information on this?

+3  A: 

Depends on the type of caching you are talking about. Opcode caching does exactly like you are saying. It takes the opcode and caches it so that whenever a user visits a particular page, that page does not need to be re-compiled if its opcode is already compiled and in the cache. If you modify a php file the caching mechanism will detect this and re-compile the code and put it in the cache.

If you're talking about caching the data on the page itself this is something different altogether.

Take a look at the Alternative PHP Cache for more info on opcode caching.

Peter D
so if i run a PHP program on my Command Prompt, can i ask that a pre-compiled version be generated too so next time it won't need to compile?
動靜能量
what is "caching the data on the page itself" isn't PHP page's content dynamic? so how can dynamic content be cached?
動靜能量
You would have to setup a specific PHP accelerator to cache what you're running. But essentially, yes, that is possible. See the accelerator's documentation for details as to how.
Ben S
APC and other accelerators cache your PHP transparently. You do not have to do anything other than install the accelerator extension and you will have all the benefits of cached PHP opcode. As for caching content on your web site, you need to decide what you want cached and for how long, so it requires a little bit more work.
Peter D
+1  A: 

What you're describing is a PHP accelerator and they do exactly what you said; store the cached, compiled bytecode so that multiple executions of the same script require only one compilation.

It's also possible to cache the results of executing the PHP script. This usually requires at least a little bit of logic, since the content of the page might have changed since it was cached. For example, you can have a look at the general cache feature provided by CodeIgniter.

Ben S
A: 

There are actually a few different forms of caching. What you're referring to is handled by packages such as eAccelerator, MMCache, etc.

While this will help some, where you'll really get a performance boost is in actually caching the HTML output where applicable, or in caching DB result sets for repetitive queries (something like memcache).

Installing any of the opcode cache mechanisms is very easy, but the other two areas of caching I referenced will gain you much larger performance benefits.

AvatarKava
+1  A: 

Peter D's answer covers opcode caching well. This can save you over 50% of page generation time (subjective) if your pages are simple!

The other caching you want to know about is the caching of data. This could be caching database result sets, a web service response, chunks of HTML or even entire pages!

A simple 'example' should illustrate:

    $cache = new Cache();
    $dataset;

    if (!$dataset == $cache->get('expensiveDataset')){

      //run code to fetch dataset from database
      $dataset = expensiveOperation();
      $cache->set('expensiveDataset', $dataset);

    }
echo $dataset; //do something with the data

There are libraries to help with object, function and page level caching. Zend Framework's Zend_Cache component is food for thought and a great implementation if you like what you see.

David Caunt