Cache Pages
Posted: Fri Jun 16, 2006 9:34 am
How can you cache pages? I can't find anything when I google cache pages OR websites
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Thanksdynamic content/processing caching?
Code: Select all
ob_start();Code: Select all
$buffer = ob_get_clean();
$hash = md5($buffer);
if(isset($_SERVER['HTTP_IF_NONE_MATCH'])){
if($hash == trim($_SERVER['HTTP_IF_NONE_MATCH'], '"')){
header("HTTP/1.x 304 Not Modified");
header('Etag: "'.$hash.'"');
exit;
}
}
header('Etag: "'.$hash.'"');
ob_start('ob_gzhandler');
print $buffer;Code: Select all
$buffer = ob_get_clean();
$hash = md5($buffer);
if(isset($_SERVER['HTTP_IF_NONE_MATCH'])){
if($hash == trim($_SERVER['HTTP_IF_NONE_MATCH'], '"')){
header("HTTP/1.x 304 Not Modified");
header('Etag: "'.$hash.'"');
exit;
}
}
header('Etag: "'.$hash.'"');
ob_start('ob_gzhandler');
print $buffer;A classic example is the use of Smarty template caching.. lets say we have a page that does some pretty intensive processing but the data isn't updated frequently, it wouldn't make sense to have it process the same data over and over each time. Instead, we write to an html file all the output of that processing and display that instead.tecktalkcm0391 wrote:Ok, will do, but what is that browser or dynamic/processing caching?
There are several ways to do this, you can set timeouts on your cached versions for instance.. or have a controller decide whether there is new content.bokehman wrote:How would you decide whether to serve the cached version or rebuild?
Code: Select all
if ($smarty->isCached())
{
if ($this->hasNewContent())
{
//do processing
}
}
// load template normallyOk, so really this is just about guessing if the content has changed; no conclusive proof. I'm not saying that is wrong, I'm just trying to clarify the point. So the only way to be certain the page hasn't changed is to test the dynamic parts, is that correct? On the solution above he said to save bandwidth, I understand the server load will be the same though. Often though there is a big mismatch in cpu to connection ratio in favour of the cpu. There certainly is in the case of my server where the connection is the bottleneck so I'm not to bothered wasting a few CPU cycles. Also my solution will save money for those paying for bandwidth.Jcart wrote:There are several ways to do this, you can set timeouts on your cached versions for instance.. or have a controller decide whether there is new content.
Do you know about....http://www.sysbotz.com/articles/phpcache.htm ? I don't have my own server, but if you do that might help.bokehman wrote:Ok, so really this is just about guessing if the content has changed; no conclusive proof. I'm not saying that is wrong, I'm just trying to clarify the point. So the only way to be certain the page hasn't changed is to test the dynamic parts, is that correct? On the solution above he said to save bandwidth, I understand the server load will be the same though. Often though there is a big mismatch in cpu to connection ratio in favour of the cpu. There certainly is in the case of my server where the connection is the bottleneck so I'm not to bothered wasting a few CPU cycles. Also my solution will save money for those paying for bandwidth.Jcart wrote:There are several ways to do this, you can set timeouts on your cached versions for instance.. or have a controller decide whether there is new content.
Anyway I'm interested in more on server side caching to cut CPU and database load as well.
I don't understand, lets say we have a news article database, and we want to check if there have been any new entries. We would simply do a quick COUNT(*) or whatever to check if there are new entries. Evidently, this would take less than fetching all of the articles and displaying them.bokehman wrote:Ok, so really this is just about guessing if the content has changed; no conclusive proof. I'm not saying that is wrong, I'm just trying to clarify the point. So the only way to be certain the page hasn't changed is to test the dynamic parts, is that correct? On the solution above he said to save bandwidth, I understand the server load will be the same though. Often though there is a big mismatch in cpu to connection ratio in favour of the cpu. There certainly is in the case of my server where the connection is the bottleneck so I'm not to bothered wasting a few CPU cycles. Also my solution will save money for those paying for bandwidth.Jcart wrote:There are several ways to do this, you can set timeouts on your cached versions for instance.. or have a controller decide whether there is new content.
Anyway I'm interested in more on server side caching to cut CPU and database load as well.
I can see your logic here but that's not what happens. In the case of an extremely high traffic site there could be any number of caching servers for every webserver and database server and those caching servers have logic that decides the caching without accessing the database. That is the whole point. The caching server has to decide do I handle this request alone or do I hand it over to the real server for a new response to be built.Jcart wrote:quick COUNT(*) or whatever to check if there are new entries.