PHPackages                             kolyunya/yii2-cacheable-widget - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. kolyunya/yii2-cacheable-widget

ActiveYii2-extension[Caching](/categories/caching)

kolyunya/yii2-cacheable-widget
==============================

Yii2 cacheable widget.

681PHP

Since Feb 8Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Kolyunya/yii2-cacheable-widget)[ Packagist](https://packagist.org/packages/kolyunya/yii2-cacheable-widget)[ RSS](/packages/kolyunya-yii2-cacheable-widget/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Yii2 cacheable widget
=====================

[](#yii2-cacheable-widget)

Description
-----------

[](#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.

Installation
------------

[](#installation)

The widget is [composer](https://getcomposer.org/)-enabled. You can aquire the latest available version from the [packagist repository](https://packagist.org/packages/kolyunya/yii2-cacheable-widget).

Usage
-----

[](#usage)

### Define a custom cacheable widget

[](#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.

```
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.

```
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

[](#render-the-widget-in-views)

```

```

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2682768?v=4)[Nick Oleynikov](/maintainers/Kolyunya)[@Kolyunya](https://github.com/Kolyunya)

---

Top Contributors

[![Kolyunya](https://avatars.githubusercontent.com/u/2682768?v=4)](https://github.com/Kolyunya "Kolyunya (10 commits)")

---

Tags

cachewidgetyiiyii2

### Embed Badge

![Health badge](/badges/kolyunya-yii2-cacheable-widget/health.svg)

```
[![Health](https://phpackages.com/badges/kolyunya-yii2-cacheable-widget/health.svg)](https://phpackages.com/packages/kolyunya-yii2-cacheable-widget)
```

###  Alternatives

[beryllium/cachebundle

Provides an interface to Memcache for Symfony2 applications

32136.0k](/packages/beryllium-cachebundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
