CHttpCacheFilter

Package system.web.filters
Inheritance class CHttpCacheFilter » CFilter » CComponent
Implements IFilter
Since 1.1.11
Source Code 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.

Public Properties

Property Type Description Defined By
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

Protected Properties

Property Type Description Defined By
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

Public Methods

Method Description Defined By
__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

Protected Methods

Method Description Defined By
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

Property Details

cacheControl property

public string $cacheControl;

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

etagSeed property

public mixed $etagSeed;

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

etagSeedExpression property

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 property read-only

protected string|boolean getEtagValue()

Gets the ETag out of either etagSeedExpression or etagSeed

lastModified property

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 property

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 property read-only

protected integer|boolean getLastModifiedValue()

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

Method Details

checkEtag() method

protected boolean checkEtag(string $etag)
$etag string the supplied etag
{return} boolean true if the supplied etag matches $etag
Source Code: framework/web/filters/CHttpCacheFilter.php#163 (show)
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() method

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
Source Code: framework/web/filters/CHttpCacheFilter.php#173 (show)
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() method

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

Generates a quoted string out of the seed

getEtagValue() method

protected string|boolean getEtagValue()
{return} string|boolean Either a quoted string serving as ETag or false if neither etagSeed nor etagSeedExpression have been set
Source Code: framework/web/filters/CHttpCacheFilter.php#149 (show)
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() method

protected integer|boolean getLastModifiedValue()
{return} integer|boolean A unix timestamp or false if neither lastModified nor lastModifiedExpression have been set
Source Code: framework/web/filters/CHttpCacheFilter.php#121 (show)
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() method

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.
Source Code: framework/web/filters/CHttpCacheFilter.php#63 (show)
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() method

protected void send304Header()
Source Code: framework/web/filters/CHttpCacheFilter.php#181 (show)
protected function send304Header()
{
    
$httpVersion=Yii::app()->request->getHttpVersion();
    
header("HTTP/$httpVersion 304 Not Modified");
}

Sends the 304 HTTP status code to the client

sendCacheControlHeader() method (available since v1.1.12)

protected void sendCacheControlHeader()
Source Code: framework/web/filters/CHttpCacheFilter.php#192 (show)
protected function sendCacheControlHeader()
{
    if(
Yii::app()->session->isStarted)
    {
        
session_cache_limiter('public');
        
header('Pragma:',true);
    }
    
header('Cache-Control: '.$this->cacheControl,true);
}

Sends the cache control header to the client

See Also

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc/api/1.1/CHttpCacheFilter