Class yii\filters\PageCache
Inheritance | yii\filters\PageCache » yii\base\ActionFilter » yii\base\Behavior » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable, yii\base\DynamicContentAwareInterface |
Uses Traits | yii\base\DynamicContentAwareTrait |
Available since version | 2.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
Property | Type | Description | Defined 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
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 |
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
Method | Description | Defined 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
Constant | Value | Description | Defined 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
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.
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().
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().
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.
Number of seconds that the data can remain valid in cache.
Use 0
to indicate that the cached data will never expire.
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).
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,
]
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
.
The view component to use for caching. If not set, the default application view component yii\web\Application::view will be used.
Method Details
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 |
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. |
---|
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 |
---|
Caches response properties.
public void cacheResponse ( ) |
protected array calculateCacheKey ( ) | ||
return | array | The key used to cache response properties. |
---|
{@inheritdoc}
public void getView ( ) |
{@inheritdoc}
public void init ( ) |
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. |