SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
May 5, 2009, 21:23 #1
- Join Date
- Feb 2003
- Location
- Australia
- Posts
- 562
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Can you check my php caching script?
I found some code on the net to cache php files. I've modified it to suit.
Does the following code look ok to you?
Is it going to be valid in all browsers?
Any other headers I need to add?
PHP Code:// Settings
$cachedir = ABSPATH . 'cache/'; // Directory to cache files in
$cachetime = 600; // Seconds to cache files for
$cacheext = 'html.gz'; // Extension to give cached files
// Ignore List
$ignore_list = array();
// 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) {
header("Last-Modified: " . gmdate("D, d M Y H:i:s \G\M\T", $cachefile_created));
header("Content-Encoding: gzip");
header("Vary: Accept-Encoding");
header("Cache-Control: private, must-revalidate, s-max-age=0");
@readfile($cachefile);
exit();
}
// If we're still here, we need to generate a cache file
ob_start();
/*************
*
*
*
* PAGE CONTENT IS LOADED HERE
*
*
*
*************/
// Now the script has run, generate a new cache file
$fp = @fopen($cachefile, 'w');
// gzip content
$cached_content = ob_get_contents();
$cached_content_gzip = gzencode($cached_content, 6);
// save the contents of output buffer to the file
@fwrite($fp, $cached_content_gzip);
@fclose($fp);
ob_end_flush();
-
May 5, 2009, 21:52 #2
I'm just lurking around to pick up some knowledge about PHP, so take what I say with a grain of salt. However, I don't think there is such thing as 'valid' PHP. There is PHP that works and PHP that doesn't work. The browser isn't going to render the PHP, just send the commands to the server.
Also, is it not working?
-
May 5, 2009, 21:59 #3
- Join Date
- Feb 2003
- Location
- Australia
- Posts
- 562
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yea it's working. I just wanted to see if there was a more efficient way to do it. Plus I wanted to make sure the headers I was submitting through would be compatible with all browsers. Browsers use the headers even though they are submitted via PHP
-
May 5, 2009, 22:06 #4
Would it make it more efficient if you were to comment out:
$ignore_list = array();
and
for ($i = 0; $i < count($ignore_list); $i++) {
$ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;
}
It seems like the for loop isn't going to run because the array is empty. So, you'd save the server some time on initializing a variable and on checking the first condition in the loop. I suppose that's a little nit picky, though :P
-
May 5, 2009, 22:13 #5
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This assumes the browser accepts gzip encoding. If it doesn't, or does but didn't ask for it, it will break horribly.
clearstatcache() is in the wrong place.
the cache-control: directive could be a bit more lenient by specifying how long. Otherwise the browser may not cache at all.
-
May 5, 2009, 22:49 #6
- Join Date
- Feb 2003
- Location
- Australia
- Posts
- 562
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for that crmalibu!
Where should the clearstatcache() function be placed?
Should max-age in the cache-control header be something like 300?
Bookmarks