PDA

View Full Version : PHP Cache?



ZigE
02-16-2009, 04:11 AM
Is it possible to cache around 500,000 pages? Each page is around 15kb~

My database isn't running as fast as I need it to be. :/

I've never really cache'd anything, so I'm not sure if this is a crazy idea. Or even possible.

ZigE
02-16-2009, 05:18 AM
Been doing a bit more reading, seems like some sort of cache system is standard for any large site

So is it basically creating a script to create hundreds of thousands of pages on the server , there's no alternative that Apache has built in or anything (?)

Blue Cat Buxton
02-16-2009, 06:47 AM
There is a tutorial in one of the articles on this site ...

http://www.websitepublisher.net/article/html-cache-php-database/

ZigE
02-18-2009, 12:41 AM
Yeah, I have/had read that. It's just the issue of keeping hundreds of thousands of static files makes me slightly uneasy.

Blue Cat Buxton
02-18-2009, 02:49 AM
Yeah, I have/had read that. It's just the issue of keeping hundreds of thousands of static files makes me slightly uneasy.

Why? Because they will become outdated or just because of the additional space on your server?

ZigE
02-18-2009, 04:02 AM
Neither particularly. Just trying to do some due diligence, so I don't cause myself any unneccesary headaches :p

Chris
02-18-2009, 09:57 AM
There is a second caching article on this site too, which is what I actually use for my literature site.

http://www.websitepublisher.net/article/html-cache-php-database/

Honestly, the storing large number of files is immaterial, use an appropriate directory structure and your site will run much faster.

The difficulty is in regularly rewriting those files. I do it once a week, but I'm not doing half a million pages. You may want to code it so you can only do it when the content is updated.

In general though, caching like this is a great idea for any database driven site that does not rely on frequently updated (aka user contributed) content.

In the end, running the script to generate the files is not more difficult for your server than having someone visit your website and request each page. Just, it'll be happening all at once, my recommendation is to code a sleep() into the php script to give it a little rest at certain intervals.

The other benefit, is when you cache, you can have more flexibility to use really powerful, complex, or inefficient SQL queries on your pages you're caching because rather than running on every page view, those queries are now just run once a day/week/month whatever.

Todd W
02-20-2009, 02:02 PM
I use a script that generates a new cache version the first time it is selected.
Then at 1am all cache files are removed.

So you may have 500k pages but if not all are accessed each day then not all are created either.

Obviously if your site is doing 500k+ page views per-day then re-writing each day and deleting daily may be a bit much unless your content needs it.

ZigE
02-26-2009, 06:01 PM
Thanks. Yeah Todd the content only really updates every month or so. But theres still some dynamic parts of the page that need to be kept dynamic, (show different ads, depending on where the user came from.)

So i'm thinking of creating an initial cache, then somehow inserting bits of dynamic code into the page. And saving the page again

hmm

ZigE
02-28-2009, 12:14 AM
Ok, so using the cache article on this site, how exactly do you parse dynamic urls through the function :confused:

Like articles.php?id=3



function get_include_contents($filename) {
if (is_file($filename)) {

ob_start();

include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
echo "no file";
return false;
}

$string = get_include_contents('/home/site/public_html/articles.php');

Chris
02-28-2009, 07:14 AM
A couple ways really.



$id = 3;

function get_include_contents($filename) {
if (is_file($filename)) {

ob_start();

include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
echo "no file";
return false;
}

$string = get_include_contents('/home/site/public_html/articles.php', $id);


Get it? Here is how I do it with my literature site. I pull a list of authors from my database and then...



while($main = mysql_fetch_array($result1)){
$author = $main[author-id];

$sidebar = get_include_contents('authorsidebargen.php', $author);


$handle = fopen("/home/lit/public_html/$author/sidebar.php", "w");

if (fwrite($handle, $sidebar) === FALSE) {
echo "Cannot write sidebar for ($author)";

}

echo "Success, wrote sidebar for ($author)";

fclose($handle);




$string = get_include_contents('../../authorgen.php', $author);





$handle = fopen("/home/lit/public_html/$author/index.php", "w");

if (fwrite($handle, $string) === FALSE) {
echo "Cannot write index for ($author)";

}

echo "Success, wrote index for ($author)";

fclose($handle);

}

function get_include_contents($filename, $author) {
if (is_file($filename)) {

ob_start();

include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
echo "no file";
return false;
}


I actually write two files for each, a sidebar, which is the left menu, and an index page, which is the normal index page + the left menu. I do this because the left menu appears on pages I cannot cache, like search result pages, so by caching it I'm also lowering the resource usage of those search result pages. Meaning, the only query done on the search pages is the search query, no standard page content queries are necessary.