Skip to content
PHP Ecosystem
Explore

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
// }
],
];
}
PropertyTypeDescriptionDefined by
$cacheControlHeaderstring|nullThe value of the Cache-Control HTTP header. Default: 'public, max-age=3600'.HttpCache
$enabledboolA value indicating whether this filter should be enabled. Default: true.HttpCache
$etagSeedcallableA PHP callback that generates the ETag seed string. Default: null.HttpCache
$exceptarrayList of action IDs that this filter should not apply to. Default: [].yii\base\ActionFilter
$lastModifiedcallableA PHP callback that returns the UNIX timestamp of the last modification time. Default: null.HttpCache
$onlyarrayList of action IDs that this filter should apply to. Default: [].yii\base\ActionFilter
$owneryii\base\T|nullThe owner of this behavior Default: null.yii\base\Behavior
$paramsmixedAdditional parameters that should be passed to the $lastModified and $etagSeed callbacks. Default: null.HttpCache
$sessionCacheLimiterstring|nullThe name of the cache limiter to be set when session_cache_limiter() is called. Default: ''.HttpCache
$weakEtagboolWhether to generate weak ETags. Default: false. Available since 2.0.8.HttpCache
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.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.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() staticReturns 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
generateEtag() protectedGenerates an ETag from the given seed string.HttpCache
getActionId() protectedReturns an action ID by converting Action::$uniqueId into an ID relative to the module.yii\base\ActionFilter
isActive() protectedReturns a value indicating whether the filter is active for the given action.yii\base\ActionFilter
sendCacheControlHeader() protectedSends the cache control header to the client.HttpCache
validateCache() protectedValidates if the HTTP cache contains valid content.HttpCache
public string|null $cacheControlHeader = 'public, max-age=3600'

The value of the Cache-Control HTTP header.

If null, the header will not be sent.

public callable $etagSeed = null

A 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.

public callable $lastModified = null

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 Action object that this filter is currently handling; $params takes the value of $params. The callback should return a UNIX timestamp.

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.

public bool $weakEtag = false

Whether 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

public function beforeAction(mixed $action): bool

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.

Parameters

ParameterTypeDescription
$actionmixedThe action to be executed.

Returns

bool: Whether the action should continue to be executed.

protected function generateEtag(string $seed): string

Generates an ETag from the given seed string.

Parameters

ParameterTypeDescription
$seedstringSeed for the ETag

Returns

string: The generated ETag

protected function sendCacheControlHeader(): mixed

Sends the cache control header to the client.

Returns

mixed

See also

protected function validateCache(int|null $lastModified, string|null $etag): bool

Validates if the HTTP cache contains valid content.

If both Last-Modified and ETag are null, returns false.

Parameters

ParameterTypeDescription
$lastModifiedint|nullThe calculated Last-Modified value in terms of a UNIX timestamp. If null, the Last-Modified header will not be validated.
$etagstring|nullThe calculated ETag value. If null, the ETag header will not be validated.

Returns

bool: Whether the HTTP cache is still valid.