Storing a static cache writes the cache to a file.
file.php
<?PHPclasscache{//static cache file suffix name ConstEXT = ' txt '; //defining cache file Storage Paths Private $_dir; Public function__construct () {$this->_dir =dirname(__file__).‘ /files/'; } Public functionCacheData ($k,$v= ",$path= ' '){ //file name $filename=$this->_dir.$path.$k.‘.‘. Self::EXT; //$v not for ': Store cache or delete cache if($v!== "){ //Delete Cache if(Is_null($v)){ return@unlink($filename); } //Storage Cache $dir=dirname($filename); if(!Is_dir($dir)){ mkdir($dir, 0777); } //Turn the $v into a string type return file_put_contents($filename, Json_encode ($v)); } //Read Cache if(!Is_file($filename)){ return false; }Else{ returnJson_decode (file_get_contents($filename),true); } }}
testfile.php
<?PHPrequire' File.php ';$data=Array( ' id ' =>1, ' name ' = ' Mary ', ' type ' and ' = 'Array(1,3,6));$file _cache=NewCache ();//Storage Cacheif($file _cache->cachedata (' Index_cache ',$data)){ Echo' Success ';}Else{ Echo' Error ';}//Read Cacheif($con=$file _cache->cachedata (' Index_cache ')){ Var_dump($con);}Else{ Echo' Error ';}//Delete Cacheif($con=$file _cache->cachedata (' Index_cache ',NULL)){ Echo' Delete Success ';}Else{ Echo' Error ';}
======
Modify it slightly, set the cache for n minutes, regenerate the cache for more than n minutes, or read the data from the cache.
In file.php, file name and file modification time are also stored in cached data when storing data.
<?PHPclasscache{//static cache file suffix name ConstEXT = ' txt '; //defining cache file Storage Paths Private $_dir; Public function__construct () {$this->_dir =dirname(__file__).‘ /files/'; } Public functionCacheData ($k,$v= ",$path= ' '){ //file name $filename=$this->_dir.$path.$k.‘.‘. Self::EXT; //$v not for ': Store cache or delete cache if($v!== "){ //Delete Cache if(Is_null($v)){ return@unlink($filename); } //Storage Cache $dir=dirname($filename); if(!Is_dir($dir)){ mkdir($dir, 0777); } //Turn the $v into a string type $_return=Array( ' filename ' =$filename, ' filetime ' = @Filemtime($filename),//file Creation (modification) time' Con ' = Json_encode ($v) ); return file_put_contents($filename, Json_encode ($_return)); } //Read Cache if(!Is_file($filename)){ return false; }Else{ returnJson_decode (file_get_contents($filename),true); } }}
testfile.php
<?PHPrequire' File.php ';$data=Array( ' id ' =>1, ' name ' = ' Mary ', ' type ' and ' = 'Array(1,3,6));$file _cache=NewCache ();//set the 5min cache, regenerate the cache more than 30s, or read the data from the cache$k= ' Index_cache ';$countdown= 5*60;$con=$file _cache->cachedata ($k);if($con){//If you can read the cache if( Time()-$con[' FILETIME '] > 30){ $file _cache->cachedata ($k,$data); Var_dump($data); }Else{ $res=$file _cache->cachedata ($k); if($res){ Var_dump(Json_decode ($res[' Con '],true)); } }}Else{//Create a cache if the cache does not exist $file _cache->cachedata ($k,$data); Var_dump($data);}
PHP Development APP Interface Summary-static cache