system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CHttpCacheFilter

system.web.filters
继承 class CHttpCacheFilter » CFilter » CComponent
实现 IFilter
可用自 1.1.11
源码 framework/web/filters/CHttpCacheFilter.php
CHttpCacheFilter implements http caching. It works a lot like COutputCache as a filter, except that content caching is being done on the client side.

公共属性

隐藏继承的属性

属性类型描述被定义在
cacheControl string Http cache control headers. CHttpCacheFilter
etagSeed mixed Seed for the ETag. CHttpCacheFilter
etagSeedExpression string|callback Expression for the ETag seed. CHttpCacheFilter
lastModified string|integer Timestamp for the last modification date. CHttpCacheFilter
lastModifiedExpression string|callback PHP Expression for the last modification date. CHttpCacheFilter

受保护的属性

隐藏继承的属性

属性类型描述被定义在
etagValue string|boolean Gets the ETag out of either etagSeedExpression or etagSeed CHttpCacheFilter
lastModifiedValue integer|boolean Gets the last modified value from either lastModifiedExpression or lastModified CHttpCacheFilter

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
filter() Performs the filtering. CFilter
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the filter. CFilter
preFilter() Performs the pre-action filtering. CHttpCacheFilter
raiseEvent() Raises an event. CComponent

受保护的方法

隐藏继承的方法

方法描述被定义在
checkEtag() Check if the etag supplied by the client matches our generated one CHttpCacheFilter
checkLastModified() Checks if the last modified date supplied by the client is still up to date CHttpCacheFilter
generateEtag() Generates a quoted string out of the seed CHttpCacheFilter
getEtagValue() Gets the ETag out of either etagSeedExpression or etagSeed CHttpCacheFilter
getLastModifiedValue() Gets the last modified value from either lastModifiedExpression or lastModified CHttpCacheFilter
postFilter() Performs the post-action filtering. CFilter
send304Header() Sends the 304 HTTP status code to the client CHttpCacheFilter
sendCacheControlHeader() Sends the cache control header to the client CHttpCacheFilter

属性详情

cacheControl 属性
public string $cacheControl;

Http cache control headers. Set this to an empty string in order to keep this header from being sent entirely.

etagSeed 属性
public mixed $etagSeed;

Seed for the ETag. Can be anything that passes through serialize().

etagSeedExpression 属性
public string|callback $etagSeedExpression;

Expression for the ETag seed. If set, this takes precedence over etagSeed.

The PHP expression will be evaluated using evaluateExpression.

A PHP expression can be any PHP code that has a value. To learn more about what an expression is, please refer to the php manual.

etagValue 属性 只读
protected string|boolean getEtagValue()

Gets the ETag out of either etagSeedExpression or etagSeed

lastModified 属性
public string|integer $lastModified;

Timestamp for the last modification date. Must be either a string parsable by strtotime() or an integer representing a unix timestamp.

lastModifiedExpression 属性
public string|callback $lastModifiedExpression;

PHP Expression for the last modification date. If set, this takes precedence over lastModified.

The PHP expression will be evaluated using evaluateExpression.

A PHP expression can be any PHP code that has a value. To learn more about what an expression is, please refer to the php manual.

lastModifiedValue 属性 只读
protected integer|boolean getLastModifiedValue()

Gets the last modified value from either lastModifiedExpression or lastModified and converts it into a unix timestamp if necessary

方法详情

checkEtag() 方法
protected boolean checkEtag(string $etag)
$etag string the supplied etag
{return} boolean true if the supplied etag matches $etag
源码: framework/web/filters/CHttpCacheFilter.php#163 (显示)
protected function checkEtag($etag)
{
    return isset(
$_SERVER['HTTP_IF_NONE_MATCH'])&&$_SERVER['HTTP_IF_NONE_MATCH']==$etag;
}

Check if the etag supplied by the client matches our generated one

checkLastModified() 方法
protected boolean checkLastModified(integer $lastModified)
$lastModified integer the last modified date
{return} boolean true if the last modified date sent by the client is newer or equal to $lastModified
源码: framework/web/filters/CHttpCacheFilter.php#173 (显示)
protected function checkLastModified($lastModified)
{
    return isset(
$_SERVER['HTTP_IF_MODIFIED_SINCE'])&&@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])>=$lastModified;
}

Checks if the last modified date supplied by the client is still up to date

generateEtag() 方法
protected string generateEtag(mixed $seed)
$seed mixed Seed for the ETag
{return} string Quoted string serving as ETag
源码: framework/web/filters/CHttpCacheFilter.php#207 (显示)
protected function generateEtag($seed)
{
    return 
'"'.base64_encode(sha1(serialize($seed),true)).'"';
}

Generates a quoted string out of the seed

getEtagValue() 方法
protected string|boolean getEtagValue()
{return} string|boolean Either a quoted string serving as ETag or false if neither etagSeed nor etagSeedExpression have been set
源码: framework/web/filters/CHttpCacheFilter.php#149 (显示)
protected function getEtagValue()
{
    if(
$this->etagSeedExpression)
        return 
$this->generateEtag($this->evaluateExpression($this->etagSeedExpression));
    elseif(
$this->etagSeed)
        return 
$this->generateEtag($this->etagSeed);
    return 
false;
}

Gets the ETag out of either etagSeedExpression or etagSeed

getLastModifiedValue() 方法
protected integer|boolean getLastModifiedValue()
{return} integer|boolean A unix timestamp or false if neither lastModified nor lastModifiedExpression have been set
源码: framework/web/filters/CHttpCacheFilter.php#121 (显示)
protected function getLastModifiedValue()
{
    if(
$this->lastModifiedExpression)
    {
        
$value=$this->evaluateExpression($this->lastModifiedExpression);
        if(
is_numeric($value)&&$value==(int)$value)
            return 
$value;
        elseif((
$lastModified=strtotime($value))===false)
            throw new 
CException(Yii::t('yii','Invalid expression for CHttpCacheFilter.lastModifiedExpression: The evaluation result "{value}" could not be understood by strtotime()',
                array(
'{value}'=>$value)));
        return 
$lastModified;
    }

    if(
$this->lastModified)
    {
        if(
is_numeric($this->lastModified)&&$this->lastModified==(int)$this->lastModified)
            return 
$this->lastModified;
        elseif((
$lastModified=strtotime($this->lastModified))===false)
            throw new 
CException(Yii::t('yii','CHttpCacheFilter.lastModified contained a value that could not be understood by strtotime()'));
        return 
$lastModified;
    }
    return 
false;
}

Gets the last modified value from either lastModifiedExpression or lastModified and converts it into a unix timestamp if necessary

preFilter() 方法
public boolean preFilter(CFilterChain $filterChain)
$filterChain CFilterChain the filter chain that the filter is on.
{return} boolean whether the filtering process should continue and the action should be executed.
源码: framework/web/filters/CHttpCacheFilter.php#63 (显示)
public function preFilter($filterChain)
{
    
// Only cache GET and HEAD requests
    
if(!in_array(Yii::app()->getRequest()->getRequestType(), array('GET''HEAD')))
        return 
true;

    
$lastModified=$this->getLastModifiedValue();
    
$etag=$this->getEtagValue();

    if(
$etag===false&&$lastModified===false)
        return 
true;

    if(
$etag)
        
header('ETag: '.$etag);

    if(isset(
$_SERVER['HTTP_IF_MODIFIED_SINCE'])&&isset($_SERVER['HTTP_IF_NONE_MATCH']))
    {
        if(
$this->checkLastModified($lastModified)&&$this->checkEtag($etag))
        {
            
$this->send304Header();
            
$this->sendCacheControlHeader();
            return 
false;
        }
    }
    elseif(isset(
$_SERVER['HTTP_IF_MODIFIED_SINCE']))
    {
        if(
$this->checkLastModified($lastModified))
        {
            
$this->send304Header();
            
$this->sendCacheControlHeader();
            return 
false;
        }
    }
    elseif(isset(
$_SERVER['HTTP_IF_NONE_MATCH']))
    {
        if(
$this->checkEtag($etag))
        {
            
$this->send304Header();
            
$this->sendCacheControlHeader();
            return 
false;
        }

    }

    if(
$lastModified)
        
header('Last-Modified: '.gmdate('D, d M Y H:i:s'$lastModified).' GMT');

    
$this->sendCacheControlHeader();
    return 
true;
}

Performs the pre-action filtering.

send304Header() 方法
protected void send304Header()
源码: framework/web/filters/CHttpCacheFilter.php#181 (显示)
protected function send304Header()
{
    
$httpVersion=Yii::app()->request->getHttpVersion();
    
header("HTTP/$httpVersion 304 Not Modified");
}

Sends the 304 HTTP status code to the client

sendCacheControlHeader() 方法 (自版本 v1.1.12 可用)
protected void sendCacheControlHeader()
源码: framework/web/filters/CHttpCacheFilter.php#192 (显示)
protected function sendCacheControlHeader()
{
    if(
Yii::app()->session->isStarted)
    {
        
Yii::app()->session->setCacheLimiter('public');
        
header('Pragma:',true);
    }
    
header('Cache-Control: '.$this->cacheControl,true);
}

Sends the cache control header to the client

参见