cache.lib.php
上传用户:feiyaoda
上传日期:2016-11-21
资源大小:9556k
文件大小:6k
- <?php
- // cache lib
- class cache_manager
- {
- // default timeout of file
- var $timeout = 3600;
-
- // last get cache expire in
- var $expire = 0;
-
- // default cache name
- var $cache_name = '';
-
- function use_cache($str)
- {
- $this->cache_name = $str;
- }
-
- function set_rule($str)
- {
- $this->timeout = $str;
- }
-
- function get_cache_signature()
- {
- if ($_COOKIE['cache_manager'])
- return "no cache status is activated. cache is currently not usedn";
-
- $ret = "page cached on ".date('r',$this->cache_time)." [expire in : ".$this->expire."] page size : ".$this->current_cache_size;
- /*
- $ret .= " <a href="".$_SERVER["REQUEST_URI"];
- if (!strpos($_SERVER["REQUEST_URI"],'?'))
- $ret .= '?';
- else
- $ret .= '&';
- $ret .= 'cache-re-render=1';
- $ret .= "">Re Render Page</a>n";
- */
- return $ret;
- }
- }
- class cache_manager_file_driver extends cache_manager
- {
- // by default where to store cache
- var $root_path = './cache/';
-
- // cache file extension
- var $cache_ext = 'patcache';
-
- function cache_manager_file_driver($path = '')
- {
- if (false != $path)
- $this->root_path = $path;
- }
-
- function delete_cache()
- {
- $dp = opendir($this->root_path);
-
- if (FALSE != $dp)
- {
- @readdir($dp);
- @readdir($dp);
- while (false !== ($file = readdir($dp)))
- {
- // deleting all files beging by $this->use_cache
- $begin_file_str = substr($file,0,strlen($this->cache_name));
-
- if ($begin_file_str == $this->cache_name)
- {
- // deleting file concerning $this->cache_name even if not expired
- delete_path($this->root_path.'/'.$file);
- }
- }
- closedir($dp);
- }
- }
-
- function save_cache($content,$render_time='')
- {
- // filename
- $expire_time = getmicrotime() + $this->timeout;
- $filename = $this->cache_name.'_'.$render_time.'_'.$expire_time.'.'.$this->cache_ext;
- $filepath = $this->root_path.$filename;
-
- $fp = fopen($filepath,'w');
-
- if (FALSE != $fp)
- {
- fwrite($fp,$content,strlen($content));
- fclose($fp);
- return true;
- }
- else trigger_error ("impossible to open cache file $filepath");
- return false;
- }
-
- /*
- * tries to get content of cache named this->cache_name
- * return false if no one is found (or expired)
- */
-
- function get_cache()
- {
- global $_INI;
-
- $get_cache_begin_time = getmicrotime();
-
- // no cache status from cookie
- if ($_COOKIE['cache-re-render'])
- {
- if ($_INI['verbose'])
- echo "<!-- cache desactivated by cookie -->n";
- return false;
- }
-
- // no cache status from url
- if ($_GET['cache-re-render'])
- {
- if ($_INI['verbose'])
- echo "<!-- cache desactivated by url -->n";
- return false;
- }
-
- // get last cache for
-
- $dp = opendir($this->root_path);
- @readdir($dp);
- @readdir($dp);
-
- // which file to return
- $ret_cache = '';
-
- // the latest time found while looking
- $last_time = 0;
-
- if (FALSE != $dp)
- {
- while (false !== ($file = readdir($dp)))
- {
- // we extract cache infos and extension
- $parts = explode('.',$file);
-
- // retriving extension
- $ext = $parts[count($parts)-1];
- unset($parts[count($parts)-1]);
-
- // rebuilding filename without extension
- $cache_info = implode('.',$parts);
-
- // if it's a valid cache file
- if ($this->cache_ext == $ext)
- {
- list($c_name,$c_render_time,$c_expire_time) = explode('_',$cache_info);
-
- // maintainance : if cache has expired we remove it
- if ($c_expire_time < $get_cache_begin_time)
- {
- delete_path($this->root_path.'/'.$file);
- }
- else
- {
- // expire time is valid
- if ($c_name == $this->cache_name)
- {
- // if expire time is the latest then it's the file we should return
- if ($last_time < $c_expire_time)
- {
- $ret_cache = $file;
- $last_time = $c_expire_time;
- $this->original_render_time = $c_render_time;
- $this->expire = timetohms(round($c_expire_time - $get_cache_begin_time)).' ('.round($c_expire_time - $get_cache_begin_time).' s)';
- }
- }
- }
- }
- }
- closedir($dp);
-
- // we have a file we could return
- if (false != $ret_cache)
- {
- $fp = fopen($this->root_path.$ret_cache,'r');
- $file_size = filesize($this->root_path.$ret_cache);
- $content = fread($fp,$file_size);
- $this->cache_time = $last_time - $get_cache_begin_time;
- $this->current_cache_size = $file_size;
- fclose($fp);
- return $content;
- }
- }
-
- return false;
- }
- }// end of class
- function new_cache($driver_type)
- {
- global $_INI;
-
- switch ($driver_type)
- {
- case 'file' : return new cache_manager_file_driver($_INI['cache_folder']);
- default : echo "error no driver named $driver_type";
- }
- }