1
Php2 /**3 * @desc File Cache4 */5 classcache{6 ConstC_file = '/runtime/';7 Private $dir= '';8 ConstEXT = '. TPL ';9 Private $filename= '';Ten Public function__construct ($dir= ''){ One $this-dir=$dir; A - } - /** the * @desc Set File cache - * @param string $key file name - * @param unkonw $data cache Data - * @param int $expire Expiration Time + */ - Public functionSet$key,$data,$expire= 0){ + $this->filename =dirname(__file__). Self::c_file.$this-dir.$key. Self::EXT; A if(file_exists($this-filename)) { at $res=$this->get ($key); - if(MD5($res) ==MD5(Json_encode ($data) ) ){ - return true; - } - } - if(!Is_dir(dirname($this-filename))) { in mkdir(dirname($this->filename), 0777); - } to + $source=fopen($this->filename, ' w+ '); - fwrite($source, Json_encode ($data)); the fclose($source); * } $ Panax Notoginseng /** - * @desc Get files the * @param string $key file name + */ A Public functionGet$key){ the //$filename = dirname (__file__). Self::c_file. $this->dir. $key. Self::ext; + if(!file_exists($this-filename)) { - return' Cache file no longer exists '; $}Else{ $ $res=file_get_contents($this-filename); - } - return $res; the } - /**Wuyi * @desc Delete files the * @param string $key file name - */ Wu Public functionDel$key){ - unlink($this-filename); About } $ - } - - $data=Array(' name ' = ' song ', ' Age ' =>20, ' sex ' = ' man ', ' favority ' =Array(' Apple ', ' banana ')); A $cache=NewCache (); + $cache->set (' Cache ',$data); the //$cache->get (' cache '); - //$cache->del (' cache ');
The above describes the simple file class, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.