dev-master
9999999-dev https://github.com/Kolyunya/yii2-cacheable-widgetYii2 cacheable widget.
GNU GPL v3.0
cache extension yii2 widget
Wallogit.com
2017 © Pedro Peláez
Yii2 cacheable widget.
A cacheable widget for Yii2 framework. Caches an entire rendered widget as a single cache item. Allows you to configure caching once in a widget class and render it in many views leaving them clean from caching business logic, ensuring DRY and KISS principles., (*1)
The widget is composer-enabled. You can aquire the latest available version from the packagist repository., (*2)
Defining a custom cacheable widget is quite simple. Just extend your widget from Kolyunya\yii2\widgets\CacheableWidget and you are good to go. You widget will be cached by a default cache (cache application component) for a default duration (for one minute) without any dependencies and without any cache key variations., (*3)
class MySimpleCacheableWidget extends CacheableWidget
{
/**
* @inheritdoc
*/
public function run()
{
// Do some resource-expensive work.
$this->doSomeExpensiveWork();
$this->doSomeMoreExpensiveWork();
// Render some heavy templates.
return $this->render('my-simple-cacheable-widget',
'foo' => $this->foo,
'bar' => $this->bar,
]);
}
}
In more complex scenarios you can override some protected methods of the base cacheable widget to configure more complex caching stategies., (*4)
class MyCacheableWidget extends CacheableWidget
{
/**
*
* @var Foo
*/
public $foo;
/**
*
* @var Bar
*/
public $bar;
/**
* @inheritdoc
*/
public function run()
{
// Do some resource-expensive work.
$this->doSomeExpensiveWork();
$this->doSomeMoreExpensiveWork();
// Render some heavy templates.
return $this->render('my-cacheable-widget',
'foo' => $this->foo,
'bar' => $this->bar,
]);
}
/**
* @inheritdoc
*/
protected function getCacheComponent()
{
return 'myCustomCache';
}
/**
* @inheritdoc
*/
protected function getCacheDuration()
{
return 60 * 60;
}
/**
* @inheritdoc
*/
protected function getCacheDependency()
{
return new MyCustomCacheDependency();
}
/**
* @inheritdoc
*/
protected function getCacheKeyVariations()
{
return [
$this->foo,
$this->bar,
];
}
}
<?= MyCacheableWidget::widget([
'foo' => $foo,
'bar' => $bar,
]); ?>
Yii2 cacheable widget.
GNU GPL v3.0
cache extension yii2 widget