ThinkPHP File Cache class code sharing,
The file cache code taken from ThinkPHP is not nonsense here. Let's take a look at the comments.
<? Php/*** @ desc File Cache */class Cache {const C_FILE = '/Runtime/'; private $ dir = ''; const EXT = '. tpl '; private $ filename = ''; public function _ construct ($ dir ='') {$ this-> dir = $ dir ;} /*** @ desc sets File Cache * @ param string $ key file name * @ param unkonw $ data cached data * @ param int $ expire expiration time */public function set ($ key, $ data, $ expire = 0) {$ this-> filename = dirname (_ FILE __). self: C_FILE. $ this-> dir. $ key. se Lf: EXT; if (file_exists ($ this-> filename) {$ res = $ this-> get ($ key); if (md5 ($ res) = md5 (json_encode ($ data) {return true ;}} if (! Is_dir (dirname ($ this-> filename) {mkdir (dirname ($ this-> filename), 0777);} $ source = fopen ($ this-> filename, 'W + '); fwrite ($ source, json_encode ($ data); fclose ($ source );} /*** @ desc get FILE * @ param string $ key FILE name */public function get ($ key) {// $ filename = dirname (_ FILE __). self: C_FILE. $ this-> dir. $ key. self: EXT; if (! File_exists ($ this-> filename) {return 'the cache file does not exist ';} else {$ res = file_get_contents ($ this-> filename);} return $ res ;} /*** @ desc delete file * @ param string $ key file name */public function del ($ key) {unlink ($ this-> filename );}} $ data = array ('name' => 'song', 'age' => 20, 'sex' => 'man ', 'favority '=> array ('apple', 'Banana'); $ cache = new Cache (); $ cache-> set ('cache', $ data ); // $ cache-> get ('cache'); // $ cache-> del ('cache ');