config->item('cache_path'); $cache_path = ($path == '') ? APPPATH.'cache/' : $path; if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path)) { log_message('error', "Unable to write cache file: ".$cache_path); return; } $uri = $CI->config->item('base_url'). $CI->config->item('index_page'). $CI->uri->uri_string(); ///////改成按照URL保存缓存 //$cache_path .= md5($uri); //生成缓存需要把@cache@refresh标识去掉 $origin_url=$this->get_origin_url($CI->config->item('index_page')); $cache_path=$cache_path.$_SERVER['HTTP_HOST'].'/'.$origin_url; create_folder_by_path(dirname($cache_path)); /////// if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE)) { log_message('error', "Unable to write cache file: ".$cache_path); return; } $expire = time() + ($this->cache_expiration * 60); if (flock($fp, LOCK_EX)) { fwrite($fp, $expire.'TS--->'.$output); fseek($fp,0,SEEK_END); fwrite($fp, ''); flock($fp, LOCK_UN); } else { log_message('error', "Unable to secure a file lock for file at: ".$cache_path); return; } fclose($fp); @chmod($cache_path, FILE_WRITE_MODE); log_message('debug', "Cache file written: ".$cache_path); } // -------------------------------------------------------------------- /** * Update/serve a cached file * * @access public * @param object config class * @param object uri class * @return void */ function _display_cache(&$CFG, &$URI) { $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path'); ///////改成按照URL保存缓存 //$filepath = $cache_path.md5($uri); //如果有@cache@refresh标识则返回false,不读取缓存 $origin_url=$this->get_origin_url($CFG->item('index_page')); $filepath=$cache_path.$_SERVER['HTTP_HOST'].'/'.$origin_url; if($this->is_has_cache_mark()) { @unlink($filepath); return false; } /////// if ( ! @file_exists($filepath)) { return FALSE; } if ( ! $fp = @fopen($filepath, FOPEN_READ)) { return FALSE; } flock($fp, LOCK_SH); $cache = ''; if (filesize($filepath) > 0) { $cache = fread($fp, filesize($filepath)); } flock($fp, LOCK_UN); fclose($fp); // Strip out the embedded timestamp if ( ! preg_match("/(\d+TS--->)/", $cache, $match)) { return FALSE; } // Has the file expired? If so we'll delete it. if (time() >= trim(str_replace('TS--->', '', $match['1']))) { if (is_really_writable($cache_path)) { @unlink($filepath); log_message('debug', "Cache file has expired. File deleted"); return FALSE; } } // Display the cache $this->_display(str_replace($match['0'], '', $cache)); log_message('debug', "Cache file is current. Sending it to browser."); return TRUE; } /** * 获取iis rewrite之前的原始url */ function get_origin_url($index_page) { if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { $origin_url = str_replace($index_page,'/',$_SERVER['HTTP_X_REWRITE_URL']); } else { $origin_url = str_replace($index_page,'/',$_SERVER['REQUEST_URI']); } $origin_url=str_replace(array('///','//','@cache@refresh'),array('/','/',''),urldecode($origin_url)); if(mb_substr($origin_url,-1,1)=='/') { $origin_url.='index.htm'; } return $origin_url; } /* * 判断是否有更新标识@cache@refresh */ function is_has_cache_mark() { //HTTP_X_REWRITE_URL 是不会带有@cache@refresh标识的,判断的时候需要判断两种url if (!isset($_SERVER['HTTP_X_REWRITE_URL'])) { if(strpos($_SERVER['REQUEST_URI'],'@cache@refresh')) { return true; } } else { if (strpos($_SERVER['HTTP_X_REWRITE_URL'],'@cache@refresh')) { return true; } } return false; } }