This article mainly introduces a PHP cache class, the file name using MD5 encryption, please refer to the use of it
The code is as follows: Dir= $dirpath; $this->make_dir ($this->dir); 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 false; The function write ($key, $data) {$filename = $this->get_filename ($key), if ($handle = fopen ($filename, ' w+ ')) {$datas = arr Ay (' 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; } function Clear_all () {$dir = $this->dir; $this->del_file ($dir);} Private Function Get_filename ($key) {return $thi S->dir. $key. ' _ '. MD5 ($key. $this->key); The Private Function Make_dir ($path) {if (! file_exists ($path)) {$this->make_dir (dirname ($path)); ($path, 0777)) Die (' Cannot create cache folder '. $path); }} Private Function Del_file ($dir) {if (Is_dir ($dir)) {$DH =openDir ($dir);//Open the 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),} else {$this->del_file ($fullpath);}} } 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);
http://www.bkjia.com/PHPjc/727577.html www.bkjia.com true http://www.bkjia.com/PHPjc/727577.html techarticle This article mainly introduces a PHP cache class, the file name using MD5 encryption, we refer to the use of bar code as follows:? phpclass cache{private $dir = data/cache/;//Define Cache directory private $ ...