Reputation: 151196
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?
Upvotes: 2
Views: 586
Reputation: 58371
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.
Upvotes: 1
Reputation: 15453
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.
Upvotes: 0
Reputation: 69382
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.
Upvotes: 2
Reputation: 4931
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.
Upvotes: 3