Cache Pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Cache Pages

Post by tecktalkcm0391 »

How can you cache pages? I can't find anything when I google cache pages OR websites
Last edited by tecktalkcm0391 on Sun Jun 18, 2006 12:26 am, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what kind of caching are you talking about? browser caching or dynamic content/processing caching?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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;
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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();
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Delete the ob_end_flush. Yes you could put this code in an external file and include it
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Ok, will do, but what is that browser or dynamic/processing caching?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Browser caching to save bandwidth. If the page is unchanged it will send a status 304
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

How would you decide whether to serve the cached version or rebuild?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
Post Reply