Class yii\filters\HttpCache
Inheritance | yii\filters\HttpCache » yii\base\ActionFilter » yii\base\Behavior » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/filters/HttpCache.php |
HttpCache implements client-side caching by utilizing the Last-Modified
and ETag
HTTP headers.
It is an action filter that can be added to a controller and handles the beforeAction
event.
To use HttpCache, declare it in the behaviors()
method of your controller class.
In the following example the filter will be applied to the index
action and
the Last-Modified header will contain the date of the last update to the user table in the database.
public function behaviors()
{
return [
[
'class' => 'yii\filters\HttpCache',
'only' => ['index'],
'lastModified' => function ($action, $params) {
$q = new \yii\db\Query();
return $q->from('user')->max('updated_at');
},
// 'etagSeed' => function ($action, $params) {
// return // generate ETag seed here
// }
],
];
}
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$cacheControlHeader | string | The value of the Cache-Control HTTP header. |
yii\filters\HttpCache |
$enabled | boolean | A value indicating whether this filter should be enabled. | yii\filters\HttpCache |
$etagSeed | callable | A PHP callback that generates the ETag seed string. | yii\filters\HttpCache |
$except | array | List of action IDs that this filter should not apply to. | yii\base\ActionFilter |
$lastModified | callable | A PHP callback that returns the UNIX timestamp of the last modification time. | yii\filters\HttpCache |
$only | array | List of action IDs that this filter should apply to. | yii\base\ActionFilter |
$owner | yii\base\Component|null | The owner of this behavior | yii\base\Behavior |
$params | mixed | Additional parameters that should be passed to the $lastModified and $etagSeed callbacks. | yii\filters\HttpCache |
$sessionCacheLimiter | string | The name of the cache limiter to be set when [session_cache_limiter()](http://www. | yii\filters\HttpCache |
$weakEtag | boolean | Whether to generate weak ETags. | yii\filters\HttpCache |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\BaseObject |
__construct() | Constructor. | yii\base\BaseObject |
__get() | Returns the value of an object property. | yii\base\BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
__set() | Sets value of an object property. | yii\base\BaseObject |
__unset() | Sets an object property to null. | yii\base\BaseObject |
afterAction() | This method is invoked right after an action is executed. | yii\base\ActionFilter |
afterFilter() | yii\base\ActionFilter | |
attach() | Attaches the behavior object to the component. | yii\base\ActionFilter |
beforeAction() | This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action. | yii\filters\HttpCache |
beforeFilter() | yii\base\ActionFilter | |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
detach() | Detaches the behavior object from the component. | yii\base\ActionFilter |
events() | Declares event handlers for the $owner's events. | yii\base\Behavior |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
init() | Initializes the object. | yii\base\BaseObject |
Protected Methods
Method | Description | Defined By |
---|---|---|
generateEtag() | Generates an ETag from the given seed string. | yii\filters\HttpCache |
getActionId() | Returns an action ID by converting yii\base\Action::$uniqueId into an ID relative to the module. | yii\base\ActionFilter |
isActive() | Returns a value indicating whether the filter is active for the given action. | yii\base\ActionFilter |
sendCacheControlHeader() | Sends the cache control header to the client. | yii\filters\HttpCache |
validateCache() | Validates if the HTTP cache contains valid content. | yii\filters\HttpCache |
Property Details
The value of the Cache-Control
HTTP header. If null, the header will not be sent.
A value indicating whether this filter should be enabled.
A PHP callback that generates the ETag seed string. The callback's signature should be:
function ($action, $params)
where $action
is the yii\base\Action object that this filter is currently handling;
$params
takes the value of $params. The callback should return a string serving
as the seed for generating an ETag.
A PHP callback that returns the UNIX timestamp of the last modification time. The callback's signature should be:
function ($action, $params)
where $action
is the yii\base\Action object that this filter is currently handling;
$params
takes the value of $params. The callback should return a UNIX timestamp.
Additional parameters that should be passed to the $lastModified and $etagSeed callbacks.
The name of the cache limiter to be set when session_cache_limiter()
is called. The default value is an empty string, meaning turning off automatic sending of cache headers entirely.
You may set this property to be public
, private
, private_no_expire
, and nocache
.
Please refer to session_cache_limiter()
for detailed explanation of these values.
If this property is null
, then session_cache_limiter()
will not be called. As a result,
PHP will send headers according to the session.cache_limiter
PHP ini setting.
Whether to generate weak ETags.
Weak ETags should be used if the content should be considered semantically equivalent, but not byte-equal.
Method Details
This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.
public boolean beforeAction ( $action ) | ||
$action | yii\base\Action | The action to be executed. |
return | boolean | Whether the action should continue to be executed. |
---|
Generates an ETag from the given seed string.
protected string generateEtag ( $seed ) | ||
$seed | string | Seed for the ETag |
return | string | The generated ETag |
---|
Sends the cache control header to the client.
See also $cacheControlHeader.
protected void sendCacheControlHeader ( ) |
Validates if the HTTP cache contains valid content.
If both Last-Modified and ETag are null, returns false.
protected boolean validateCache ( $lastModified, $etag ) | ||
$lastModified | integer | The calculated Last-Modified value in terms of a UNIX timestamp. If null, the Last-Modified header will not be validated. |
$etag | string | The calculated ETag value. If null, the ETag header will not be validated. |
return | boolean | Whether the HTTP cache is still valid. |
---|