A method of generating static cache file by wordpress without plug-in
Speaking of wordpress generating static cache? The first thing that comes to mind is the WP Super Cache plug-in. Indeed, most wordpress webmasters are using it.
But there will always be people who are obsessed with perfectionism, and there are also many people who don’t like to use plug-ins. Today, the method of generating static cache files by wordpress without plug-ins can get rid of the use of this powerful wordpress cache plug-in WP Super Cache for everyone!
First of all, let’s talk about the working principle of WP Super Cache. The Mod_rewrite acceleration mode of this plug-in has a very good acceleration effect, because this mode directly generates static html pages from the pages of the website, and then forwards the requests to static files by using Mod_rewrite, thus skipping the php parsing step and directly spitting out the pages to the browser, thus achieving the acceleration effect.
The tutorial introduced today is almost the same as the above working principle, and with the use of acceleration classes such as CDN, the effect will be very obvious, and the speed will be 100%!
Operation tutorial
PHP Code copies the content to the clipboard.
php define('CACHE_ROOT', dirname(__FILE__).'/cache'); define('CACHE_LIFE', 86400); //Lifetime of cache file, in seconds, 86400 seconds is one day. define('CACHE_SUFFIX','.html'); //The extension of the cache file, never use .php.asp.jsp.pl and so on. $file_name = md5($_SERVER['REQUEST_URI']).CACHE_SUFFIX; //Cache file name //Cache directory, which disperses cache files according to the first two digits of md5. Avoid too many files. If necessary, you can use the third and fourth digits as the name, and add another layer of directory. //256 directories with 1000 files in each directory, it is 250,000 pages. A two-tier directory is 65536*1000= 65 million. //Don't make a single directory more than 1000, so as not to affect performance. $cache_dir = CACHE_ROOT.'/'.substr($file_name,0,2); $cache_file = $cache_dir.'/'.$file_name; //Cache file storage path If ($ _ server ['request _ method'] = =' get') {//Get mode is used to request caching, and after POST, you generally want to see the latest results. If (file _ exists ($ cache _ file)&& time ()-filemtime ($ cache _ file) < cache _ life) {//If the cache file exists and has not expired, read it out. $fp = fopen($cache_file,'rb'); fpassthru($fp); fclose($fp); exit(); } elseif(! file_exists($cache_dir)){ if(! file_exists(CACHE_ROOT)){ mkdir(CACHE_ROOT,0777); chmod(CACHE_ROOT,0777); } mkdir($cache_dir,0777); chmod($cache_dir,0777); } Function auto _ cache ($ contents) {//callback function, which is automatically called when the program ends. global $cache_file; $fp = fopen($cache_file,'wb'); fwrite($fp,$contents); fclose($fp); chmod($cache_file,0777); clean_old_cache(); //When a new cache is generated, all the old caches are automatically deleted. To save space. return $contents; } function clean_old_cache(){ chdir(CACHE_ROOT); foreach (glob("*/*".CACHE_SUFFIX) as $file){ if(time()-filemtime($file)>CACHE_LIFE){
unlink($file);
}
}
}
ob_start(‘auto_cache’); //callback function auto_cache
}
else{
The if (file _ exists ($ cache _ file)) {//file _ exists () function checks whether the file or directory exists.
unlink($cache_file); //Delete the cache file without the request of GET.
}
}
? >
There are comments in the code. Save the code as a cache.php file, and then put it in the root directory of the website. Remember, it is the root directory of the website, not the root directory of wordpress theme! Then create a cache folder for the root directory of the website and give 777 write permission.
And then put the code
The code is as follows:
require(‘cache.php’);
Index.php’s After php In this way, static files will be automatically generated in the cache file.