Class yii\filters\PageCache

Inheritanceyii\filters\PageCache » yii\base\ActionFilter » yii\base\Behavior » yii\base\BaseObject
Implementsyii\base\Configurable, yii\base\DynamicContentAwareInterface
Uses Traitsyii\base\DynamicContentAwareTrait
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/filters/PageCache.php

PageCache implements server-side caching of whole pages.

It is an action filter that can be added to a controller and handles the beforeAction event.

To use PageCache, declare it in the behaviors() method of your controller class. In the following example the filter will be applied to the index action and cache the whole page for maximum 60 seconds or until the count of entries in the post table changes. It also stores different versions of the page depending on the application language.

public function behaviors()
{
    return [
        'pageCache' => [
            'class' => 'yii\filters\PageCache',
            'only' => ['index'],
            'duration' => 60,
            'dependency' => [
                'class' => 'yii\caching\DbDependency',
                'sql' => 'SELECT COUNT(*) FROM post',
            ],
            'variations' => [
                \Yii::$app->language,
            ]
        ],
    ];
}

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$cache yii\caching\CacheInterface|array|string The cache object or the application component ID of the cache object. yii\filters\PageCache
$cacheCookies boolean|array A boolean value indicating whether to cache all cookies, or an array of cookie names indicating which cookies can be cached. yii\filters\PageCache
$cacheHeaders boolean|array A boolean value indicating whether to cache all HTTP headers, or an array of HTTP header names (case-insensitive) indicating which HTTP headers can be cached. yii\filters\PageCache
$dependency array|yii\caching\Dependency The dependency that the cached content depends on. yii\filters\PageCache
$duration integer Number of seconds that the data can remain valid in cache. yii\filters\PageCache
$enabled boolean Whether to enable the page cache. yii\filters\PageCache
$except array List of action IDs that this filter should not apply to. yii\base\ActionFilter
$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
$variations string[]|string List of factors that would cause the variation of the content being cached. yii\filters\PageCache
$varyByRoute boolean Whether the content being cached should be differentiated according to the route. yii\filters\PageCache
$view yii\base\View The view component to use for caching. yii\filters\PageCache

Public Methods

Hide inherited methods

MethodDescriptionDefined 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
addDynamicPlaceholder() {@inheritdoc} yii\base\DynamicContentAwareTrait
afterAction() This method is invoked right after an action is executed. yii\base\ActionFilter
afterFilter() yii\base\ActionFilter
afterRestoreResponse() This method is invoked right after the response restoring is finished (but before the response is sent). yii\filters\PageCache
attach() {@inheritdoc} 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\PageCache
beforeCacheResponse() This method is invoked right before the response caching is to be started. yii\filters\PageCache
beforeFilter() yii\base\ActionFilter
cacheResponse() Caches response properties. yii\filters\PageCache
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() {@inheritdoc} yii\base\ActionFilter
events() Declares event handlers for the $owner's events. yii\base\Behavior
getDynamicPlaceholders() {@inheritdoc} yii\base\DynamicContentAwareTrait
getView() {@inheritdoc} yii\filters\PageCache
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() {@inheritdoc} yii\filters\PageCache
setDynamicPlaceholders() {@inheritdoc} yii\base\DynamicContentAwareTrait

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
calculateCacheKey() yii\filters\PageCache
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
restoreResponse() Restores response properties from the given data. yii\filters\PageCache
updateDynamicContent() Replaces placeholders in $content with results of evaluated dynamic statements. yii\base\DynamicContentAwareTrait

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
PAGE_CACHE_VERSION 1 Page cache version, to detect incompatibilities in cached values when the data format of the cache changes. yii\filters\PageCache

Property Details

$cache public property

The cache object or the application component ID of the cache object. After the PageCache object is created, if you want to change this property, you should only assign it with a cache object. Starting from version 2.0.2, this can also be a configuration array for creating the object.

$cacheCookies public property (available since version 2.0.4)

A boolean value indicating whether to cache all cookies, or an array of cookie names indicating which cookies can be cached. Be very careful with caching cookies, because it may leak sensitive or private data stored in cookies to unwanted users.

See also \yii\filters\insertResponseCollectionIntoData().

public boolean|array $cacheCookies false
$cacheHeaders public property (available since version 2.0.4)

A boolean value indicating whether to cache all HTTP headers, or an array of HTTP header names (case-insensitive) indicating which HTTP headers can be cached. Note if your HTTP headers contain sensitive information, you should white-list which headers can be cached.

See also \yii\filters\insertResponseCollectionIntoData().

public boolean|array $cacheHeaders true
$dependency public property

The dependency that the cached content depends on. This can be either a yii\caching\Dependency object or a configuration array for creating the dependency object. For example,

[
    'class' => 'yii\caching\DbDependency',
    'sql' => 'SELECT MAX(updated_at) FROM post',
]

would make the output cache depend on the last modified time of all posts. If any post has its modification time changed, the cached content would be invalidated.

If $cacheCookies or $cacheHeaders is enabled, then yii\caching\Dependency::$reusable should be enabled as well to save performance. This is because the cookies and headers are currently stored separately from the actual page content, causing the dependency to be evaluated twice.

$duration public property

Number of seconds that the data can remain valid in cache. Use 0 to indicate that the cached data will never expire.

public integer $duration 60
$enabled public property

Whether to enable the page cache. You may use this property to turn on and off the page cache according to specific setting (e.g. enable page cache only for GET requests).

public boolean $enabled true
$variations public property

List of factors that would cause the variation of the content being cached. Each factor is a string representing a variation (e.g. the language, a GET parameter). The following variation setting will cause the content to be cached in different versions according to the current application language:

[
    Yii::$app->language,
]
public string[]|string $variations null
$varyByRoute public property

Whether the content being cached should be differentiated according to the route. A route consists of the requested controller ID and action ID. Defaults to true.

public boolean $varyByRoute true
$view public property

The view component to use for caching. If not set, the default application view component yii\web\Application::view will be used.

public yii\base\View $view null

Method Details

afterRestoreResponse() public method (available since version 2.0.11)

This method is invoked right after the response restoring is finished (but before the response is sent).

You may override this method to do last-minute preparation before the response is sent.

public void afterRestoreResponse ( $data )
$data array|null

An array of an additional data stored in a cache entry or null.

beforeAction() public method

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.

beforeCacheResponse() public method (available since version 2.0.11)

This method is invoked right before the response caching is to be started.

You may override this method to cancel caching by returning false or store an additional data in a cache entry by returning an array instead of true.

public boolean|array beforeCacheResponse ( )
return boolean|array

Whether to cache or not, return an array instead of true to store an additional data.

cacheResponse() public method (available since version 2.0.3)

Caches response properties.

public void cacheResponse ( )
calculateCacheKey() protected method (available since version 2.0.3)

protected array calculateCacheKey ( )
return array

The key used to cache response properties.

getView() public method

{@inheritdoc}

public void getView ( )
init() public method

{@inheritdoc}

public void init ( )
restoreResponse() protected method (available since version 2.0.3)

Restores response properties from the given data.

protected void restoreResponse ( $response, $data )
$response yii\web\Response

The response to be restored.

$data array

The response property data.