cache.lib.php
Upload User: feiyaoda
Upload Date: 2016-11-21
Package Size: 9556k
Code Size: 6k
Category:

WEB Mail

Development Platform:

PHP

  1. <?php
  2. // cache lib
  3. class cache_manager
  4. {
  5.     // default timeout of file
  6.     var $timeout    = 3600;
  7.     
  8.     // last get cache expire in
  9.     var $expire     = 0;    
  10.     
  11.     // default cache name
  12.     var $cache_name = '';
  13.     
  14.     function use_cache($str)
  15.     {
  16.         $this->cache_name = $str;
  17.     }
  18.     
  19.     function set_rule($str)
  20.     {
  21.         $this->timeout = $str;
  22.     }
  23.     
  24.     function get_cache_signature()
  25.     {
  26.         if ($_COOKIE['cache_manager'])
  27.             return "no cache status is activated. cache is currently not usedn";            
  28.             
  29.         $ret  = "page cached on ".date('r',$this->cache_time)." [expire in : ".$this->expire."] page size : ".$this->current_cache_size;
  30.         /*
  31.         $ret .= " <a href="".$_SERVER["REQUEST_URI"];        
  32.         if (!strpos($_SERVER["REQUEST_URI"],'?')) 
  33.             $ret .= '?';
  34.         else 
  35.             $ret .= '&';
  36.         $ret .= 'cache-re-render=1';
  37.         $ret .= "">Re Render Page</a>n";
  38.         */
  39.         return $ret;
  40.     }
  41. }
  42. class cache_manager_file_driver extends cache_manager
  43. {
  44.     // by default where to store cache
  45.     var $root_path = './cache/';
  46.     
  47.     // cache file extension
  48.     var $cache_ext = 'patcache';
  49.     
  50.     function cache_manager_file_driver($path = '')
  51.     {
  52.         if (false != $path)
  53.             $this->root_path = $path;
  54.     }
  55.     
  56.     function delete_cache()
  57.     {
  58.         $dp = opendir($this->root_path);
  59.         
  60.         if (FALSE != $dp)
  61.         {
  62.             @readdir($dp);
  63.             @readdir($dp);
  64.             while (false !== ($file = readdir($dp)))
  65.             {
  66.                 // deleting all files beging by $this->use_cache
  67.                 $begin_file_str = substr($file,0,strlen($this->cache_name));
  68.                 
  69.                 if ($begin_file_str == $this->cache_name)
  70.                 {
  71.                     // deleting file concerning $this->cache_name even if not expired
  72.                     delete_path($this->root_path.'/'.$file);
  73.                 }
  74.             }
  75.             closedir($dp);
  76.         }
  77.     }    
  78.     
  79.     function save_cache($content,$render_time='')
  80.     {
  81.         // filename
  82.         $expire_time = getmicrotime() + $this->timeout;
  83.         $filename = $this->cache_name.'_'.$render_time.'_'.$expire_time.'.'.$this->cache_ext;
  84.         $filepath = $this->root_path.$filename;
  85.         
  86.         $fp = fopen($filepath,'w');
  87.         
  88.         if (FALSE != $fp)
  89.         {
  90.             fwrite($fp,$content,strlen($content));
  91.             fclose($fp);
  92.             return true;
  93.         }
  94.         else trigger_error ("impossible to open cache file $filepath");
  95.         return false;
  96.     }
  97.     
  98.     /*
  99.      * tries to get content of cache named this->cache_name
  100.      * return false if no one is found (or expired) 
  101.      */ 
  102.     
  103.     function get_cache()
  104.     {
  105.         global $_INI;
  106.         
  107.         $get_cache_begin_time = getmicrotime();
  108.         
  109.         // no cache status from cookie
  110.         if ($_COOKIE['cache-re-render'])
  111.         {
  112.             if ($_INI['verbose'])
  113.                 echo "<!-- cache desactivated by cookie -->n";
  114.             return false;
  115.         }
  116.             
  117.         // no cache status from url
  118.         if ($_GET['cache-re-render'])
  119.         {
  120.             if ($_INI['verbose'])
  121.                 echo "<!-- cache desactivated by url -->n";
  122.             return false;
  123.         }
  124.             
  125.         // get last cache for 
  126.         
  127.         $dp = opendir($this->root_path);
  128.         @readdir($dp);
  129.         @readdir($dp);
  130.         
  131.         // which file to return
  132.         $ret_cache = '';
  133.         
  134.         // the latest time found while looking
  135.         $last_time = 0;
  136.         
  137.         if (FALSE != $dp)
  138.         {
  139.             while (false !== ($file = readdir($dp)))
  140.             {
  141.                 // we extract cache infos and extension
  142.                 $parts = explode('.',$file);
  143.                 
  144.                 // retriving extension
  145.                 $ext = $parts[count($parts)-1];
  146.                 unset($parts[count($parts)-1]);
  147.                 
  148.                 // rebuilding filename without extension
  149.                 $cache_info = implode('.',$parts);
  150.                 
  151.                 // if it's a valid cache file
  152.                 if ($this->cache_ext == $ext)
  153.                 {
  154.                     list($c_name,$c_render_time,$c_expire_time) = explode('_',$cache_info);
  155.                     
  156.                     // maintainance : if cache has expired we remove it
  157.                     if ($c_expire_time < $get_cache_begin_time)
  158.                     {
  159.                         delete_path($this->root_path.'/'.$file);
  160.                     }
  161.                     else
  162.                     {
  163.                         // expire time is valid
  164.                         if ($c_name == $this->cache_name)
  165.                         {
  166.                             // if expire time is the latest then it's the file we should return
  167.                             if ($last_time < $c_expire_time)
  168.                             {
  169.                                 $ret_cache = $file;
  170.                                 $last_time = $c_expire_time;
  171.                                 $this->original_render_time = $c_render_time;
  172.                                 $this->expire = timetohms(round($c_expire_time - $get_cache_begin_time)).' ('.round($c_expire_time - $get_cache_begin_time).' s)';
  173.                             }
  174.                         }
  175.                     }
  176.                 }
  177.             }
  178.             closedir($dp);
  179.             
  180.             // we have a file we could return
  181.             if (false != $ret_cache)
  182.             {
  183.                 $fp = fopen($this->root_path.$ret_cache,'r');
  184.                 $file_size = filesize($this->root_path.$ret_cache);
  185.                 $content = fread($fp,$file_size);
  186.                 $this->cache_time = $last_time - $get_cache_begin_time;
  187.                 $this->current_cache_size = $file_size;
  188.                 fclose($fp);
  189.                 return $content;
  190.             }
  191.         }
  192.         
  193.         return false;
  194.     }
  195. }// end of class
  196. function new_cache($driver_type)
  197. {
  198.     global $_INI;
  199.     
  200.     switch ($driver_type)
  201.     {
  202.         case 'file' : return new cache_manager_file_driver($_INI['cache_folder']);
  203.         default : echo "error no driver named $driver_type";
  204.     }
  205. }