public function _write_cache($output)
{
$CI =& get_instance();
$path = $CI->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();
if (($cache_query_string = $CI->config->item('cache_query_string')) && !empty($_SERVER['QUERY_STRING'])) {
if (is_array($cache_query_string)) {
$uri .= '?' . http_build_query(array_intersect_key($_GET, array_flip($cache_query_string)));
} else {
$uri .= '?' . $_SERVER['QUERY_STRING'];
}
}
$cache_path .= md5($uri);
if (!($fp = @fopen($cache_path, 'w+b'))) {
log_message('error', 'Unable to write cache file: ' . $cache_path);
return;
}
if (flock($fp, LOCK_EX)) {
// If output compression is enabled, compress the cache
// itself, so that we don't have to do that each time
// we're serving it
if ($this->_compress_output === TRUE) {
$output = gzencode($output);
if ($this->get_header('content-type') === NULL) {
$this->set_content_type($this->mime_type);
}
}
$expire = time() + $this->cache_expiration * 60;
// Put together our serialized info.
$cache_info = serialize(array('expire' => $expire, 'headers' => $this->headers));
$output = $cache_info . 'ENDCI--->' . $output;
for ($written = 0, $length = strlen($output); $written < $length; $written += $result) {
if (($result = fwrite($fp, substr($output, $written))) === FALSE) {
break;
}
}
flock($fp, LOCK_UN);
} else {
log_message('error', 'Unable to secure a file lock for file at: ' . $cache_path);
return;
}
fclose($fp);
if (is_int($result)) {
chmod($cache_path, 0640);
log_message('debug', 'Cache file written: ' . $cache_path);
// Send HTTP cache-control headers to browser to match file cache settings.
$this->set_cache_header($_SERVER['REQUEST_TIME'], $expire);
} else {
@unlink($cache_path);
log_message('error', 'Unable to write the complete cache content at: ' . $cache_path);
}
}