CI_Output::_display_cache PHP Method

_display_cache() public method

Update/serve cached output
public _display_cache ( &$CFG, &$URI ) : boolean
return boolean TRUE on success or FALSE on failure
    public function _display_cache(&$CFG, &$URI)
    {
        $cache_path = $CFG->item('cache_path') === '' ? APPPATH . 'cache/' : $CFG->item('cache_path');
        // Build the file path. The file name is an MD5 hash of the full URI
        $uri = $CFG->item('base_url') . $CFG->item('index_page') . $URI->uri_string;
        if (($cache_query_string = $CFG->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'];
            }
        }
        $filepath = $cache_path . md5($uri);
        if (!file_exists($filepath) or !($fp = @fopen($filepath, 'rb'))) {
            return FALSE;
        }
        flock($fp, LOCK_SH);
        $cache = filesize($filepath) > 0 ? fread($fp, filesize($filepath)) : '';
        flock($fp, LOCK_UN);
        fclose($fp);
        // Look for embedded serialized file info.
        if (!preg_match('/^(.*)ENDCI--->/', $cache, $match)) {
            return FALSE;
        }
        $cache_info = unserialize($match[1]);
        $expire = $cache_info['expire'];
        $last_modified = filemtime($filepath);
        // Has the file expired?
        if ($_SERVER['REQUEST_TIME'] >= $expire && is_really_writable($cache_path)) {
            // If so we'll delete it.
            @unlink($filepath);
            log_message('debug', 'Cache file has expired. File deleted.');
            return FALSE;
        } else {
            // Or else send the HTTP cache control headers.
            $this->set_cache_header($last_modified, $expire);
        }
        // Add headers from cache file.
        foreach ($cache_info['headers'] as $header) {
            $this->set_header($header[0], $header[1]);
        }
        // Display the cache
        $this->_display(substr($cache, strlen($match[0])));
        log_message('debug', 'Cache file is current. Sending it to browser.');
        return TRUE;
    }

Usage Example

Exemplo n.º 1
0
 function _display_cache(&$CFG, &$URI)
 {
     if ($this->is_logged_in) {
         // user is logged in, so don't execute the normal _display_cache
         return false;
     } else {
         // not logged in so lets try and display the cache as normal
         return parent::_display_cache($CFG, $URI);
     }
 }
All Usage Examples Of CI_Output::_display_cache