2017 © Pedro Peláez
 

yii2-extension yii2-cacheable-widget

Yii2 cacheable widget.

image

kolyunya/yii2-cacheable-widget

Yii2 cacheable widget.

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Yii2 cacheable widget

Description

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)

Installation

The widget is composer-enabled. You can aquire the latest available version from the packagist repository., (*2)

Usage

Define a custom cacheable widget

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,
        ];
    }
}

Render the widget in views

<?= MyCacheableWidget::widget([
    'foo' => $foo,
    'bar' => $bar,
]); ?>

The Versions

08/02 2017

dev-master

9999999-dev https://github.com/Kolyunya/yii2-cacheable-widget

Yii2 cacheable widget.

  Sources   Download

GNU GPL v3.0

cache extension yii2 widget