bpbp Posted October 9, 2007 Share Posted October 9, 2007 Hi I found this script on the net and I think its great it caches my dynamic mysql content in to static pages to reduce load on the server. There is one problem I can't figure out.. When i run the script it shows the dynamic mysql content plus content from the cache right below it. I have settings in place where if file cache is not old then it should load, but for some reason the dynamic content loads anyway. Any thoughs on how i can prevent the script to show the dynamic mysql content, when the cache file is still valid? <html> <body> <hr> Header <hr> <? // PHP Fast File Cache v0.1b // Written by Åke Wallebom (ake at ake nu) // Free to use for non-commercial purposes $cache_cacheDirectory = '/home/class/public_html/cache/'; if (!isset($cache_refreshTime)) { $cache_refreshTime = 5; // seconds to keep a cached copy } ///// // No configuration beyond this line ///// function cache_fopen_exclusive($id) { if ($fp = fopen($id, "ab+")) { if (flock($fp, LOCK_EX | LOCK_NB)) { return $fp; } else { fclose($fp); } } return false; } function cache_fclose_exclusive($fp) { flock($fp, LOCK_UN); fclose($fp); } // Create cache file name if (isset($cache_parameters) && is_array($cache_parameters)) { $cache_id_param = ''; foreach($cache_parameters as $value) { if (isset($_REQUEST[$value])) { $cache_id_param = $cache_id_param . '_' . $value . '=' . $_REQUEST[$value]; } } $cache_id = md5($_SERVER['REQUEST_URI']) . '_' . md5($cache_id_param); } else { $cache_id = md5($_SERVER['REQUEST_URI']); } $cache_cacheFile = $cache_cacheDirectory . $cache_id . '.cache.html'; $cache_FileExists = file_exists($cache_cacheFile); $cache_FileIsFresh = $cache_FileExists && (time() - filemtime($cache_cacheFile) < $cache_refreshTime); // Check if cache file is usable or not if ($cache_FileIsFresh) { $cache_UseCache = true; } else { $cache_UseCache = false; if ($cache_fp_lock = cache_fopen_exclusive($cache_cacheFile . '.lock')) { // We got the lock, create new cache $cache_UpdateCache = true; } else { // We didn't get the lock, use old file or regenerate $cache_UseCache = $cache_FileExists; $cache_UpdateCache = false; } } // We will update the cache file and should not abort if the user cancels the request if ($cache_UpdateCache) { ignore_user_abort(true); } // Output cached content if ($cache_UseCache) { // Display the cached file readfile($cache_cacheFile); //exit; } else { // We need to regenerate the page... ob_start(); } ?> Hello World<br> Here is the content that is needed to be cached. <? // PHP Fast File Cache v0.1b // Written by Åke Wallebom (ake at ake nu) // Free to use for non-commercial purposes // Get all output data and save to cache file if ($cache_UpdateCache) { if ($cache_fp = fopen($cache_cacheFile . '.new', "wb")) { $cache_data = ob_get_contents(); fwrite($cache_fp, $cache_data, strlen($cache_data)); fclose($cache_fp); rename($cache_cacheFile . '.new', $cache_cacheFile); } // Close and remove the lock-file cache_fclose_exclusive($cache_fp_lock); $cache_er_old = error_reporting(0); unlink($cache_cacheFile . '.lock'); error_reporting($cache_er_old); // The script can now be aborted by the user without any problems ignore_user_abort(false); } if (!$cache_UseCache) ob_end_flush(); ?> <hr> Footer <hr> Link to comment https://forums.phpfreaks.com/topic/72399-php-partial-content-caching-script/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.