CHttpCacheFilter
包 | system.web.filters |
---|---|
继承 | class CHttpCacheFilter » CFilter » CComponent |
实现 | IFilter |
可用自 | 1.1.11 |
源码 | framework/web/filters/CHttpCacheFilter.php |
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
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 |
属性详情
Http cache control headers. Set this to an empty string in order to keep this header from being sent entirely.
Seed for the ETag. Can be anything that passes through serialize().
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.
Gets the ETag out of either etagSeedExpression or etagSeed
Timestamp for the last modification date. Must be either a string parsable by strtotime() or an integer representing a unix timestamp.
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.
Gets the last modified value from either lastModifiedExpression or lastModified and converts it into a unix timestamp if necessary
方法详情
protected boolean checkEtag(string $etag)
| ||
$etag | string | the supplied etag |
{return} | boolean | true if the supplied etag matches $etag |
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
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 |
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
protected string generateEtag(mixed $seed)
| ||
$seed | mixed | Seed for the ETag |
{return} | string | Quoted string serving as ETag |
protected function generateEtag($seed)
{
return '"'.base64_encode(sha1(serialize($seed),true)).'"';
}
Generates a quoted string out of the seed
protected string|boolean getEtagValue()
| ||
{return} | string|boolean | Either a quoted string serving as ETag or false if neither etagSeed nor etagSeedExpression have been set |
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
protected integer|boolean getLastModifiedValue()
| ||
{return} | integer|boolean | A unix timestamp or false if neither lastModified nor lastModifiedExpression have been set |
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
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. |
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.
protected void send304Header()
|
protected function send304Header()
{
$httpVersion=Yii::app()->request->getHttpVersion();
header("HTTP/$httpVersion 304 Not Modified");
}
Sends the 304 HTTP status code to the client
protected void sendCacheControlHeader()
|
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