Simple PHP Cache class sharing

Source: Internet
Author: User
Tags flock md5 md5 encryption php class

  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; &nbsp.}  }   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));    IF (! mkdir ($path, 0777))    die (' Cannot create cache folder '. $path);  } &nbsp  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);  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.