PHP simple cache, how to assign the page code to the variable?

Source: Internet
Author: User
Tags crc32
You can see a good cache class in the open source file, as follows: {code ...} then create a simple php file: {code ...} the following is a question: $ contentdate (& #039; H: I: sjSF & #039;); what should I do if I change to the PHO page content? {Code...} shows a good cache class in the open source file, as follows:


  Dir_isvalid ($ dir) {$ this-> dir = $ dir; $ this-> lifetime = $ lifetime; $ this-> ext = '. php '; $ this-> cacheid = $ this-> getcacheid () ;}/ *** check whether the cache is valid */private function isvalid () {if (! File_exists ($ this-> cacheid) return false; if (! (@ $ Mtime = filemtime ($ this-> cacheid) return false; if (mktime ()-$ mtime> $ this-> lifetime) return false; return true ;} /*** write cache ** $ mode = 0. obtain the page content in browser cache * $ mode = 1 and assign values directly (received by the $ content parameter) obtain the page content in the form of * $ mode = 2, and obtain the page content locally (fopen ile_get_contents) (it seems that this method is unnecessary) */public function write ($ mode = 0, $ content = '') {switch ($ mode) {case 0: $ content = ob_get_contents (); break; default: brea K;} ob_end_flush (); try {file_put_contents ($ this-> cacheid, $ content);} catch (Exception $ e) {$ this-> error ('write cache failed! Check the directory permission! ') ;}}/*** Load cache * exit () after loading the cache, terminate the execution of the original page program. if the cache is invalid, run the original page program to generate the cache * ob_start () enable browser Cache to get page content at the end of the page */public function load () {if ($ this-> isvalid () {echo "This is Cache. "; // which of the following two methods is better ????? Require_once ($ this-> cacheid); // echo file_get_contents ($ this-> cacheid); exit () ;}else {ob_start ();}} /*** clear cache */public function clean () {try {unlink ($ this-> cacheid);} catch (Exception $ e) {$ this-> error ('failed to clear the cached file! Check the directory permission! ') ;}}/*** Obtain the cache file path */private function getcacheid () {return $ this-> dir. md5 ($ this-> geturl ()). $ this-> ext;}/*** check whether the directory exists or can be created */private function dir_isvalid ($ dir) {if (is_dir ($ dir) return true; try {mkdir ($ dir, 0777);} catch (Exception $ e) {$ this-> error ('The specified cache directory does not exist and creation failed! Check the directory permission! '); Return false;} return true;}/*** get the complete url of the current page */private function geturl () {$ url = ''; if (isset ($ _ SERVER ['request _ URI ']) {$ url = $ _ SERVER ['request _ URI'];} else {$ url = $ _ SERVER ['php _ SELF ']; $ url. = empty ($ _ SERVER ['query _ string'])? '':'? '. $ _ SERVER ['query _ string'];} return $ url;}/*** output error message */private function error ($ str) {echo'

'. $ Str .'

';}}?>

Create a simple php file by yourself:


  Load () ;}// page code start $ content = date ('H: I: s jS f'); echo $ content; // page code end if (CACHEENABLE) {$ cache-> write (1, $ content); // The first time the cache is running or expires, the cache is generated}?>

The following is a question: $ content = date ('H: I: s jS f'); what should I do if I change to the PHO page content?

$ Content = <      
      
      
      
      Bootstrap 101 template.ap-southeast-1.maxcompute.aliyun-inc.com/api    
      
      
      
      
          
      
      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.