Class yii\filters\PageCache

Inheritanceyii\filters\PageCache » yii\base\ActionFilter » yii\base\Behavior » yii\base\Object
Implementsyii\base\Configurable
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\Cache|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 The owner of this behavior yii\base\Behavior
$variations array 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\Object
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
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\Behavior
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
beforeFilter() yii\base\ActionFilter
cacheResponse() Caches response properties. yii\filters\PageCache
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
detach() Detaches the behavior object from the component. yii\base\Behavior
events() Declares event handlers for the $owner's events. yii\base\Behavior
hasMethod() Returns a value indicating whether a method is defined. yii\base\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the object. yii\filters\PageCache

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
calculateCacheKey() yii\filters\PageCache
isActive() Returns a value indicating whether the filer is active for the given action. yii\base\ActionFilter
restoreResponse() Restores response properties from the given data 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.

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

$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 depends on the last modified time of all posts. If any post has its modification time changed, the cached content would be invalidated.

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

$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).

$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,
]
$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.

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

Method Details

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.

boolean beforeAction$action )
$action yii\base\Action

The action to be executed.

return boolean

Whether the action should continue to be executed.

cacheResponse() public method (available since version 2.0.3)

Caches response properties.

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

array calculateCacheKey( )
return array

The key used to cache response properties.

init() public method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

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

Restores response properties from the given data

void restoreResponse$response$data )
$response yii\web\Response

The response to be restored

$data array

The response property data