In php page cache, ob functions are mainly used, such as ob_start (), ob_end_flush (), ob_get_contents (), but more advanced caching does not use these functions, the last implementation in this article is mentioned... in php page cache, ob functions are mainly used, such as ob_start (), ob_end_flush (), ob_get_contents (), but more advanced caching does not use these functions, the last implementation in this article is described. you can refer to it.
Ob_start (): indicates the start mark of the page cache. the content of this function is stored in the page cache until ob_end_flush () or ob_end_clean;
Ob_get_contents (): used to obtain the content in the page cache. after obtaining the content, we can do whatever we want to do. we can filter fields and match the content ~~~
Ob_end_flush (): indicates that the page cache is complete. after my verification, the cached content is output to the current page, that is, the cached content can be displayed.
With these three php functions, you can implement powerful functions. if the database query volume is large, you can use cache to solve this problem.
The following is the encoding section.
1. the initialization function is generally used to set the page cache path and the cache file naming format. you can customize it according to your preferences. the ID used here is the encrypted $ _ SERVER [REQUEST_URI] parameter, this function has an if judgment. if the cache period is not passed, the cached file is loaded. Otherwise, the source file is loaded. the code is as follows:
Function page_init () {$ url = $ _ SERVER ['request _ URI ']; // suburl. this parameter is generally unique $ pageid = md5 ($ url ); $ dir = str_replace ('/', '_', substr ($ _ SERVER ['script _ name'], 1,-4); // Directory naming method, for example, exp_index if (! File_exists ($ pd = PAGE_PATH. $ dir. '/') @ mkdir ($ pd, 0777) or die ("$ pd directory creation failed "); // such as cache/page/exp_index/define ('page _ file', $ pd. $ pageid. '.html '); // such as cache/page/exp_index/tables $ contents = file_get_contents (PAGE_FILE); // read if ($ contents & substr ($ contents, 13, 10)> time () // corresponds to the custom header in the page_cache () function {echo substr ($ contents, 27); exit (0);} return true ;}
2. page cache function. here we use a technique to add a header information-expiration time in the content of the cached file. Therefore, you only need to compare the expiration time in the header with the current time each time, in the page_init () function, you can determine whether the cache has expired. the code is as follows:
Function page_cache ($ ttl = 0) {$ ttl = $ ttl? $ Ttl: PAGE_TTL; // The cache time. the default value is 3600 s. $ contents = ob_get_contents (). // Obtain the content from the cache. $ contents ="
N ". $ contents; // add the custom header: Expiration Time = generation time + cache time file_put_contents (PAGE_FILE, $ contents); // write the ob_end_flush () in the cache file (); // release the cache}
3. use the function. Note that the two functions are executed in sequence, and do not forget ob_start (). The code is as follows:
Example 2: Below is an example to illustrate the PHP page cache technology. the code is as follows:
For example, the code is as follows:
Read_cache () reads the cache and outputs * @ example $ cache-> creatre_cache () creates a cache file (placed before the file ends) * @ example $ cache-> list_file () return to the list of all cached files * @ example $ cache-> del_file () delete all cached files */class Esj_Cache {private $ cache_folder = null; // The cacher folder private $ wroot_dir = null; // site directory private $ cacher_create_time = null; // the creation time of the cacher file public function _ construct ($ cache_foldername, $ cacher_time = 100) {ob_start (); $ this-> wroot_dir = $ _ SERVER ['document _ root']; $ this-> cache_folder = $ cache_foldername; $ this-> cacher_create_time = $ cacher_time;} public function read_cache () {try {if (self: create_folder ($ this-> cache_folder) {self: get_cache (); // output cache file information} else {echo "failed to create cache folder! "; Return false ;}} catch (Exception $ e) {echo $ e; return false ;}// test whether the cache folder has private function exist_folder ($ foler) {if (file_exists ($ this-> wroot_dir. "/". $ foler) {return true;} else {return false ;}// create a new folder private function create_folder ($ foler) {if (! Self: exist_folder ($ foler) {try {mkdir ($ this-> wroot_dir. "/". $ foler, 0777); chmod ($ this-> wroot_dir. "/". $ foler, 0777); return true;} catch (Exception $ e) {self: get_cache (); // output cache return false ;} else {return true ;}// read the cache file private function get_cache () {$ file_name = self: get_filename (); if (file_exists ($ file_name) & (filemtime ($ file_name) + $ this-> cacher_create_t Ime)> time () {$ content = file_get_contents ($ file_name); if ($ content) {echo $ content; ob_end_flush (); exit ;} else {echo "file reading failed"; exit ;}}// the name of the returned file private function get_filename () {$ filename = $ file_name = $ this-> wroot_dir. '/'. $ this-> cache_folder. '/'. md5 ($ _ SERVER ['query _ string']). ". html "; return $ filename;} // create the public function create_cache () {$ filename = self: get_file Name (); if ($ filename! = "") {Try {file_put_contents ($ filename, ob_get_contents (); return true;} catch (Exception $ e) {echo "write cache failed :". $ e; exit () ;}return true ;}// obtain all files in the cache. public function list_file () {$ path = $ this-> cache_folder; if ($ handle = opendir ($ path) {while (false! ==( $ File = readdir ($ handle) {if ($ file! = "." & $ File! = ".. ") {$ Path1 = $ path. "/". $ file; if (file_exists ($ path1) {$ result [] = $ file ;}} closedir ($ handle);} return $ result ;} // delete all files in the cache. public function del_file () {$ path = $ this-> cache_folder; if ($ handle = opendir ($ path) {while (false! ==( $ File = readdir ($ handle) {if ($ file! = "." & $ File! = ".. ") {$ Path1 = $ path. "/". $ file; if (file_exists ($ path1) {unlink ($ path1) ;}} closedir ($ handle) ;}return true ;}}?>
Tutorial address:
Reprinted! But please include the article address ^