09c06-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 52 行
PHP
52 行
<?php// A function to call that will set up & handle the output caching:// The one optional parameter is to specify how many seconds must pass// before the page is recreated (defaults to 1 hour)function cache_page($refresh = 3600) { // First of all generate the filename for this cache file. Base it upon // the filename plus GET and POST variables, and use sha1() to hash it // to a 40 character string so we don't run over filename length limits: $hash = sha1($_SERVER['PHP_SELF'] . '|G|' . serialize($_GET) . '|P|' . serialize($_POST)); // Create the actual filename of where to find this. It will be in a // subdirectory called 'cache' below the location of this library. $file = dirname(__FILE__) . '/cache/' . $hash; // Now that we've done all that, see if the last modified time is // less than the current time plus the refresh time if ((time() - @filemtime($file)) < $refresh) { // We are done, just return the file and exit readfile($file); exit(); } else { // Otherwise, we have to actually create the page and save it. // To do this, first stop the user from aborting at this point so // it will definately complete. ignore_user_abort(); // Now set up a shutdown function, so that we can ensure we actually // finish up properly when the script exits (for any reason) register_shutdown_function('_cache_page_exit', $file); // Now, to finish, we need to start output buffering ob_start(); }}// Create the shutdown function that will be called for us when the script// exits, allowing us to clean up everything.function _cache_page_exit($file) { // We were buffering and are done. First step is to get the contents // and display it to the user. $output = ob_get_flush(); // Flush all buffers so the user can get the data while we finish up. flush(); // Save this data into the cache file, blowing away current contents // if they existed. Request an exclusive lock for the writing just // so that two copies of this don't interfere with each other. file_put_contents($file, $output, LOCK_EX);}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?