cache_vars = array(); $this->cache_age = time(); } function cache_by_vars($timeout, $cache_vars = array()) { parent::cache($timeout); $this->set_cache_vars($cache_vars); } function set_cache_vars($cache_vars) { $this->cache_vars = array(); if (is_array($cache_vars)) { foreach ($cache_vars as $var => $value) { $this->add_cache_var($var, $value); } } else { $this->add_cache_var(NULL, $cache_vars) ; echo "hi."; } } function add_cache_var($var, $value) { if ($var == NULL) { $this->cache_vars[] = $value; //printf("
next = %s", $value) ; } else { $this->cache_vars[$var] = $value; //printf("
%s = %s", $var, $value) ; } } function cache_override() { return; } /** * Write a Cache File * * @access public * @return void */ function _write_cache($output) { $filepath = $this->_cache_filename(); $cache_path = dirname($filepath); if ( ! is_dir($cache_path) OR ! is_writable($cache_path)) { return; } if ( ! $fp = @fopen($filepath, 'wb')) { log_message('error', "Unable to write cache file: ".$filepath); return; } $expire = time() + ($this->cache_expiration * 60); flock($fp, LOCK_EX); fwrite($fp, $expire.'TS--->'.$output); flock($fp, LOCK_UN); fclose($fp); @chmod($filepath, 0777); log_message('debug', "Cache file written: ". $filepath); } /** * Invalidate a Cache File * * @access public * @return bool */ function clear_cache() { $cleared = TRUE; if (file_exists($this->_cache_filename())) { $cleared = @unlink($this->_cache_filename()); } return $cleared; } // -------------------------------------------------------------------- /** * Update/serve a cached file * * @access public * @return void */ function display_cache($cache_vars = array()) { if (!empty($cache_vars)) { $this->set_cache_vars = $cache_vars; } $filepath = $this->_cache_filename(); $cache_path = dirname($filepath); /* CHAMPS: * Removed some useless checks to see if $cache_path exists and is writable * * also dropped the file_exists check, because @fopen() will handle that, and it's not a special error. */ if ( ! $fp = @fopen($filepath, 'rb')) { 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']))) { @unlink($filepath); log_message('debug', "Cache file has expired. File deleted"); return FALSE; } // Display the cache // CHAMPS: I think this wasn't necessary with the old code because cache_expiration never got set. $this->cache_expiration = 0; $this->_display(str_replace($match['0'], '', $cache)); log_message('debug', "Cache file is current. Sending it to browser."); $this->cache_age = filemtime($filepath); return TRUE; } function _cache_filename() { $CI =& get_instance(); $cache_dir = $CI->config->item('cache_dir'); if ($cache_dir == '') { $cache_dir = BASEPATH . 'cache/'; } if (empty($this->cache_vars)) { $key = $CI->config->item('base_url'). $CI->config->item('index_page'). $CI->uri->uri_string(); } else { $key = ''; ksort($this->cache_vars); foreach ($this->cache_vars as $key => $value) { $key = $key . '=' . $value . '&'; } } if (ENCODE_VARS) { $key = md5($key); } return $cache_dir . $key; } } ?>