PHP File Cache class

Source: Internet
Author: User

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 ');

PHP File Cache class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.