This article mainly introduces a PHP cache class, file name using MD5 encryption, we refer to the use of the bar
The code is as follows: <?php class cache { private $dir = "data/cache/";//define Cache directory private $key = ' c_a_sss ';//filename MD5 encryption key function Set_dir ($dirpath) { $this->dir= $dirpath; $this->make_dir ($ THIS->DIR); &NBSP} function Read ($key, $minutes =1) { $filename = $this->get_filename ($key); if ($datas = @file_get_contents ($filename)) { $datas = Unserialize ($datas); if (Time ()-$datas [' time] '] < $minutes *60 { return $datas [' data ']; } } return FAL Se &NBSP} function write ($key, $data) { $filename = $this->get_filename ($key); IF ($handle = fopen ($filename, ' w+ ')) { $datas = array (' Data ' => $data, ' time ' =>time ()); flock ($handle, LOCK_EX); $rs = fputs ($handle, serialize ($datas)); flock ($handle, Lock_un); fclose ($handle); if ($rs!==false) {return true;  .} } return false; &NBSP} function Clear_all () { $dir = $this->dir; $this->del_file ($dir); nbsp Private Function Get_filename ($key) { return $this->dir. $key. ' _ '. MD5 ($key. $this->key); &NBSP} private function Make_dir ($path) { if (! file_exists ($path)) { $this ; Make_dir (DirName ($path)); &NBSP;IF (! mkdir ($path, 0777)) die (' Cannot create cache folder '. $path); }   private function Del_file ($dir) { if (Is_dir ($dir)) { $DH =opendir ($dir);//Open Directory//list all files in the directory and remove them. And.. while (false!== ($file = Readdir ($DH)) { if ($file!= "." && $file!= "..." { $fullpath = $dir. " /". $file; if (!is_dir ($fullpath)) { unlink ($fullpath); &NBSP} else { $this->del_file ($fullpath); } } &NBSP} closedir ($DH); } } } $cache = new Cache ( ); $cache->set_dir (' data/cache_dir/'); $data = $cache->read (' sys ', 1); if (empty ($data)) { $data =array (' AA ' =>1111, ' BB ' =>2222, ' Date ' =>date (' y-m-d h:i:s ') ); $cache->write (' sys ', $data); } Print_r ($data);