Page 1 of 2

Cache Pages

Posted: Fri Jun 16, 2006 9:34 am
by tecktalkcm0391
How can you cache pages? I can't find anything when I google cache pages OR websites

Posted: Fri Jun 16, 2006 9:36 am
by John Cartwright
what kind of caching are you talking about? browser caching or dynamic content/processing caching?

Posted: Fri Jun 16, 2006 9:49 am
by tecktalkcm0391
I am not sure exactly what.

I think its browser caching...Just like where it caches the content of the page, like the whole page CSS's, JS's, and images, so I can save bandwidth. What is:
dynamic content/processing caching?
Thanks

Posted: Fri Jun 16, 2006 10:02 am
by bokehman
One possible way:

Start the script like this:

Code: Select all

ob_start();
and end it like this:

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;

Posted: Fri Jun 16, 2006 10:04 am
by tecktalkcm0391
What is that for? What type?

I can just put the

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;
in an included file right?

cause I already start everypage with ob_start();

and end it with ob_flush();

Posted: Fri Jun 16, 2006 10:08 am
by bokehman
Delete the ob_end_flush. Yes you could put this code in an external file and include it

Posted: Fri Jun 16, 2006 10:14 am
by tecktalkcm0391
Ok, will do, but what is that browser or dynamic/processing caching?

Posted: Fri Jun 16, 2006 10:30 am
by bokehman
Browser caching to save bandwidth. If the page is unchanged it will send a status 304

Posted: Fri Jun 16, 2006 10:42 am
by John Cartwright
tecktalkcm0391 wrote:Ok, will do, but what is that browser or dynamic/processing caching?
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.

Posted: Fri Jun 16, 2006 10:50 am
by bokehman
How would you decide whether to serve the cached version or rebuild?

Posted: Fri Jun 16, 2006 10:53 am
by John Cartwright
bokehman wrote:How would you decide whether to serve the cached version or rebuild?
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.

Code: Select all

if ($smarty->isCached())
{
   if ($this->hasNewContent())
   {
      //do processing
   }
}

// load template normally

Posted: Fri Jun 16, 2006 12:19 pm
by bokehman
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.
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.

Anyway I'm interested in more on server side caching to cut CPU and database load as well.

Posted: Fri Jun 16, 2006 12:25 pm
by tecktalkcm0391
bokehman wrote:
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.
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.

Anyway I'm interested in more on server side caching to cut CPU and database load as well.
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.

Posted: Fri Jun 16, 2006 12:48 pm
by John Cartwright
bokehman wrote:
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.
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.

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.

Posted: Fri Jun 16, 2006 1:06 pm
by bokehman
Jcart wrote:quick COUNT(*) or whatever to check if there are new entries.
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.