HttpCache
API reference › yii › filters › HttpCache
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// } ], ];}Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$cacheControlHeader | string| | The value of the Cache-Control HTTP header. Default: 'public,. | HttpCache |
$enabled | bool | A value indicating whether this filter should be enabled. Default: true. | HttpCache |
$etagSeed | callable | A PHP callback that generates the ETag seed string. Default: null. | HttpCache |
$except | array | List of action IDs that this filter should not apply to. Default: []. | yii\ |
$lastModified | callable | A PHP callback that returns the UNIX timestamp of the last modification time. Default: null. | HttpCache |
$only | array | List of action IDs that this filter should apply to. Default: []. | yii\ |
$owner | yii\ | The owner of this behavior Default: null. | yii\ |
$params | mixed | Additional parameters that should be passed to the $lastModified and $etagSeed callbacks. Default: null. | HttpCache |
$sessionCacheLimiter | string| | The name of the cache limiter to be set when session_cache_limiter() is called. Default: ''. | HttpCache |
$weakEtag | bool | Whether to generate weak ETags. Default: false. Available since 2.0.8. | HttpCache |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
afterAction() | This method is invoked right after an action is executed. | yii\ |
afterFilter() | yii\ | |
attach() | Attaches the behavior object to the component. | yii\ |
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. | HttpCache |
beforeFilter() | yii\ | |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detach() | Detaches the behavior object from the component. | yii\ |
events() | Declares event handlers for the $owner’s events. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
generateEtag() protected | Generates an ETag from the given seed string. | HttpCache |
getActionId() protected | Returns an action ID by converting Action::$uniqueId into an ID relative to the module. | yii\ |
isActive() protected | Returns a value indicating whether the filter is active for the given action. | yii\ |
sendCacheControlHeader() protected | Sends the cache control header to the client. | HttpCache |
validateCache() protected | Validates if the HTTP cache contains valid content. | HttpCache |
Property details
Section titled “Property details”$cacheControlHeader
Section titled “$cacheControlHeader ”public string|null $cacheControlHeader = 'public, max-age=3600'The value of the Cache-Control HTTP header.
If null, the header will not be sent.
$etagSeed
Section titled “$etagSeed ”public callable $etagSeed = nullA PHP callback that generates the ETag seed string.
The callback’s signature should be:
function ($action, $params)where $action is the 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.
$lastModified
Section titled “$lastModified ”public callable $lastModified = nullA 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 Action object that this filter is currently handling;
$params takes the value of $params. The callback should return a UNIX timestamp.
$sessionCacheLimiter
Section titled “$sessionCacheLimiter ”public string|null $sessionCacheLimiter = ''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.
$weakEtag
Section titled “$weakEtag ”public bool $weakEtag = falseWhether to generate weak ETags.
Weak ETags should be used if the content should be considered semantically equivalent, but not byte-equal.
Since
2.0.8
Method details
Section titled “Method details”beforeAction()
Section titled “beforeAction() ”public function beforeAction(mixed $action): boolThis 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$action | mixed | The action to be executed. |
Returns
bool: Whether the action should continue to be executed.
generateEtag()
Section titled “generateEtag() ”protected function generateEtag(string $seed): stringGenerates an ETag from the given seed string.
Parameters
| Parameter | Type | Description |
|---|---|---|
$seed | string | Seed for the ETag |
Returns
string: The generated ETag
sendCacheControlHeader()
Section titled “sendCacheControlHeader() ”protected function sendCacheControlHeader(): mixedSends the cache control header to the client.
validateCache()
Section titled “validateCache() ”protected function validateCache(int|null $lastModified, string|null $etag): boolValidates if the HTTP cache contains valid content.
If both Last-Modified and ETag are null, returns false.
Parameters
| Parameter | Type | Description |
|---|---|---|
$lastModified | int| | 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. |
Returns
bool: Whether the HTTP cache is still valid.