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)