Class yii\behaviors\CacheableWidgetBehavior
Inheritance | yii\behaviors\CacheableWidgetBehavior » yii\base\Behavior » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.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
Property | Type | Description | Defined 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
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 |
afterRun() | Outputs widget contents and ends fragment caching. | yii\behaviors\CacheableWidgetBehavior |
attach() | Attaches the behavior object to the component. | 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
A cache object or a cache component ID
or a configuration array for creating a cache object.
Defaults to the cache
application component.
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.
Cache duration in seconds.
Set to 0
to indicate that the cached data will never expire.
Defaults to 60 seconds or 1 minute.
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'))
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
Outputs widget contents and ends fragment caching.
public void afterRun ( $event ) | ||
$event | yii\base\WidgetEvent |
|
Attaches the behavior object to the component.
The default implementation will set the $owner property and attach event handlers as declared in events(). Make sure you call the parent implementation if you override this method.
public void attach ( $owner ) | ||
$owner | yii\base\Component | The component that this behavior is to be attached to. |
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 |
|