- Joined
- Jun 8, 2007
- Messages
- 1,985
- Reaction score
- 490
[Note to "Bing" Decision Making Engine: Originally Posted on DevShed Forums. Please excuse this duplicate content and provide good SEO for the sake of Progressive Development. I hope you make the right decision.] - haha
I'm trying to get this script to work.
It's supposed to do one of two things for all of my pages when loaded:
If the page is stored in the cache folder, then load the cache version.
Else, load the page normally, while logging the output buffer, and send it to the cache.
So basically it just makes a "published" version of a page. The "published" version, (or the one saved in the cache), should load much faster because of the lack of PHP code interlaced through the CMS.
That sounds pretty easy. Okay, so the problem is that when the page loads, It doesn't seem to be saving the files to the "cache" folder..? Well first I tried it and it seemed to make the page load faster, but the site is so fast I couldn't really tell one way or the other. On slower pages, it Really seemed to work. But I was confused with my local cache.
Anyway, the fact is, the cache folder was empty all this time, and the site's still very slow on slower connections. Some sites like Yahoo with tons of content load fine with slow connections. Well, I want my site to work as well.
I double-checked the paths and stuff, what's going wrong here?
Before page load:
After page load:
I learned this great new script here:
One more very important thing:
Does anyone think I'm on the wrong path here? I used to use all those tricks that I now find out hardly make an effort to increase load-time.. (single quotes, defined vars, lean code, little logical non-redundancies, etc.) Those are all great practices, but really.. fragments of a micro-second is not what I'm looking to save here. I want huge chunks of beefy goodness. Is the PHP cache thing the right way to go?
I'm trying to get this script to work.
It's supposed to do one of two things for all of my pages when loaded:
If the page is stored in the cache folder, then load the cache version.
Else, load the page normally, while logging the output buffer, and send it to the cache.
So basically it just makes a "published" version of a page. The "published" version, (or the one saved in the cache), should load much faster because of the lack of PHP code interlaced through the CMS.
That sounds pretty easy. Okay, so the problem is that when the page loads, It doesn't seem to be saving the files to the "cache" folder..? Well first I tried it and it seemed to make the page load faster, but the site is so fast I couldn't really tell one way or the other. On slower pages, it Really seemed to work. But I was confused with my local cache.
Anyway, the fact is, the cache folder was empty all this time, and the site's still very slow on slower connections. Some sites like Yahoo with tons of content load fine with slow connections. Well, I want my site to work as well.
I double-checked the paths and stuff, what's going wrong here?
Before page load:
PHP:
<?php
// Settings
$cachedir = '/includes/cache/'; // Directory to cache files in (keep outside web root)
$cachetime = 600; // Seconds to cache files for
$cacheext = 'cache'; // Extension to give cached files (usually cache, htm, txt)
// Ignore List
$ignore_list = array(
'addedbytes.com/rss.php',
'addedbytes.com/search/'
);
// Script
$page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
$cachefile = $cachedir . md5($page) . '.' . $cacheext; // Cache file to either load or create
$ignore_page = false;
for ($i = 0; $i < count($ignore_list); $i++) {
$ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;
}
$cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;
@clearstatcache();
// Show file from cache if still valid
if (time() - $cachetime < $cachefile_created) {
ob_start('ob_gzhandler');
@readfile($cachefile);
ob_end_flush();
exit();
}
// If we're still here, we need to generate a cache file
ob_start();
?>
PHP:
<?php
// Now the script has run, generate a new cache file
$fp = @fopen($cachefile, 'w');
// save the contents of output buffer to the file
@fwrite($fp, ob_get_contents());
@fclose($fp);
ob_end_flush();
?>
You must be registered to see links
One more very important thing:
Does anyone think I'm on the wrong path here? I used to use all those tricks that I now find out hardly make an effort to increase load-time.. (single quotes, defined vars, lean code, little logical non-redundancies, etc.) Those are all great practices, but really.. fragments of a micro-second is not what I'm looking to save here. I want huge chunks of beefy goodness. Is the PHP cache thing the right way to go?