Php 像codeigniter这样的框架是如何进行页面缓存的?

Php 像codeigniter这样的框架是如何进行页面缓存的?,php,model-view-controller,codeigniter,outputcache,Php,Model View Controller,Codeigniter,Outputcache,我知道codeigniter似乎会将视图输出保存指定的分钟数,如果用户在这么多分钟内再次请求该页面,它将为该页面的保存版本提供服务,而不是再次处理该请求。它似乎将视图中的所有输出保存在一个文件中,但它是如何做到这一点的?然后,它如何知道这些缓存文件的过期时间 除此之外,如何为使用这种缓存模型的页面创建登录安全性 如有任何见解,将不胜感激。谢谢 在core/中CodeIgniter.php的第177行: if ($EXT->_call_hook('cache_override') === F

我知道codeigniter似乎会将视图输出保存指定的分钟数,如果用户在这么多分钟内再次请求该页面,它将为该页面的保存版本提供服务,而不是再次处理该请求。它似乎将视图中的所有输出保存在一个文件中,但它是如何做到这一点的?然后,它如何知道这些缓存文件的过期时间

除此之外,如何为使用这种缓存模型的页面创建登录安全性


如有任何见解,将不胜感激。谢谢

在core/中CodeIgniter.php的第177行:

if ($EXT->_call_hook('cache_override') === FALSE)
    {
        if ($OUT->_display_cache($CFG, $URI) == TRUE)
        {
            exit;
        }
    }
它检查缓存文件并显示该文件,而不是处理控制器/操作代码

您还可以读取输出类在显示缓存文件之前是如何检查该文件是否过期的

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;

    $filepath = $cache_path.md5($uri);

    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;
}

在core/中CodeIgniter.php的第177行:

if ($EXT->_call_hook('cache_override') === FALSE)
    {
        if ($OUT->_display_cache($CFG, $URI) == TRUE)
        {
            exit;
        }
    }
它检查缓存文件并显示该文件,而不是处理控制器/操作代码

您还可以读取输出类在显示缓存文件之前是如何检查该文件是否过期的

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;

    $filepath = $cache_path.md5($uri);

    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;
}

代替模板系统,它可能会使用
ob_start
和friends来捕获和保存输出。使缓存工作的首选方法是,如果:和自:起修改,则遵循条件HTTP头
。虽然我不知道CodeIgniter是否真的做到了这一点,或者仅仅依赖于预配置的超时。作为模板系统的替代,它可能会使用
ob_start
和friends来捕获和保存输出。使缓存工作的首选方法是,如果:
和自:起修改,则遵循条件HTTP头
。虽然我不知道CodeIgniter是否真的这样做了,或者仅仅依赖于预先配置的超时。有趣的是,但是如果页面应该在登录之后呢。如果它不能进入控制器,开发人员如何才能添加会话权限检查?而且,每次都要进行preg_匹配并替换整个文件内容似乎是一件很遗憾的事情,必须有更好的方法来确定过期时间。有趣的是,如果页面应该在登录之后呢。如果它不能进入控制器,开发人员如何才能添加会话权限检查?而且,每次都要进行preg_匹配并替换整个文件内容似乎是一种耻辱,必须有更好的方法来确定过期时间。