PHP uses filesystem functions to cache operations (generate, Fetch, delete operations)

Source: Internet
Author: User
Tags delete cache

1<?PHP2    class File{3        //$key is equivalent to the file name of the cache4 //$value cache data5        Private $_dir;//Define default Path6        Constext= '. txt ';7         Public function__construct () {8              $this->_dir=dirname(__file__).‘ /files/'; 9          }   Ten         Public functionCacheData ($key,$value="",$path=""){ One          $filename=$this->_dir.$path.$key. Self::EXT; A          if($value!==""){//write $value value to cache -              if(Is_null($value)){ -                  return@unlink($filename);//Delete cache, @ The function of the output of the Mask error message the              } -              $dir=dirname($filename); -              if(!Is_dir($dir)){ -                  mkdir($dir, 0777); +              } -         return file_put_contents($filename, Json_encode ($value));  +          } A          //Get Cache at          if(!Is_file($filename)){ -              return false; -}Else{ -             returnJson_decode (file_get_contents($filename),true); -          } -        } in    } -?>
<?PHPrequire_once("file.php"); $data=Array(        ' id ' =>1, ' name ' = ' sina ', ' type ' and ' = 'Array(4,5,6), ' Test ' =Array(1,45,67=>Array(123, ' Tsysa '),    ); $file=New File(); //Index_mk_cache Cache file name//$data for cached data    if($file->cachedata (' Index_mk_cache ',$data)){//the effect of implementing output cache files when $data is changed to null        Echo"Success"; }Else{        Echo"Error"; }?>

PHP uses filesystem functions to cache operations (generate, Fetch, delete operations)

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.