Class yii\behaviors\CacheableWidgetBehavior

Inheritanceyii\behaviors\CacheableWidgetBehavior » yii\base\Behavior » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0.14
Source Code https://github.com/yiisoft/yii2/blob/master/framework/behaviors/CacheableWidgetBehavior.php

Cacheable widget behavior automatically caches widget contents according to duration and dependencies specified.

The behavior may be used without any configuration if an application has cache component configured. By default the widget will be cached for one minute.

The following example will cache the posts widget for an indefinite duration until any post is modified.

use yii\behaviors\CacheableWidgetBehavior;

public function behaviors()
{
    return [
        [
            'class' => CacheableWidgetBehavior::className(),
            'cacheDuration' => 0,
            'cacheDependency' => [
                'class' => 'yii\caching\DbDependency',
                'sql' => 'SELECT MAX(updated_at) FROM posts',
            ],
        ],
    ];
}

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$cache yii\caching\CacheInterface|string|array A cache object or a cache component ID or a configuration array for creating a cache object. yii\behaviors\CacheableWidgetBehavior
$cacheDependency yii\caching\Dependency|array|null A cache dependency or a configuration array for creating a cache dependency or null meaning no cache dependency. yii\behaviors\CacheableWidgetBehavior
$cacheDuration integer Cache duration in seconds. yii\behaviors\CacheableWidgetBehavior
$cacheEnabled boolean Whether to enable caching or not. yii\behaviors\CacheableWidgetBehavior
$cacheKeyVariations string[]|string An array of strings or a single string which would cause the variation of the content being cached (e.g. an application language, a GET parameter). yii\behaviors\CacheableWidgetBehavior
$owner yii\base\Component|null The owner of this behavior yii\base\Behavior

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
afterRun() Outputs widget contents and ends fragment caching. yii\behaviors\CacheableWidgetBehavior
attach() {@inheritdoc} yii\behaviors\CacheableWidgetBehavior
beforeRun() Begins fragment caching. Prevents owner widget from execution if its contents can be retrieved from the cache. yii\behaviors\CacheableWidgetBehavior
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() 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\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject

Property Details

$cache public property

A cache object or a cache component ID or a configuration array for creating a cache object. Defaults to the cache application component.

$cacheDependency public property

A cache dependency or a configuration array for creating a cache dependency or null meaning no cache dependency.

For example,

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

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

$cacheDuration public property

Cache duration in seconds. Set to 0 to indicate that the cached data will never expire. Defaults to 60 seconds or 1 minute.

public integer $cacheDuration 60
$cacheEnabled public property

Whether to enable caching or not. Allows to turn the widget caching on and off according to specific conditions. The following configuration will disable caching when a special GET parameter is passed:

empty(Yii::$app->request->get('disable-caching'))
public boolean $cacheEnabled true
$cacheKeyVariations public property

An array of strings or a single string which would cause the variation of the content being cached (e.g. an application 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,
]

Method Details

afterRun() public method

Outputs widget contents and ends fragment caching.

public void afterRun ( $event )
$event yii\base\WidgetEvent

Widget::EVENT_AFTER_RUN event.

attach() public method

{@inheritdoc}

public void attach ( $owner )
$owner
beforeRun() public method

Begins fragment caching. Prevents owner widget from execution if its contents can be retrieved from the cache.

public void beforeRun ( $event )
$event yii\base\WidgetEvent

Widget::EVENT_BEFORE_RUN event.