PHPackages                             pozitronik/yii2-cached-properties-trait - 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. pozitronik/yii2-cached-properties-trait

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

pozitronik/yii2-cached-properties-trait
=======================================

Support to cache yii2 models properties

1.0.3(3y ago)05GPL-3.0-or-laterPHPPHP &gt;=8.0

Since Aug 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/pozitronik/yii2-cached-properties-trait)[ Packagist](https://packagist.org/packages/pozitronik/yii2-cached-properties-trait)[ RSS](/packages/pozitronik-yii2-cached-properties-trait/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (8)Versions (5)Used By (0)

yii2-cached-properties-trait
============================

[](#yii2-cached-properties-trait)

Support to cache yii2 models properties

[![GitHub Workflow Status](https://camo.githubusercontent.com/82cf9f43a6c1e27b3a99d64a5381684b23b10265a2c0ed0e3dec5ba95a5a0ebc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f706f7a6974726f6e696b2f796969322d6361636865642d70726f706572746965732d74726169742f434925323077697468253230506f737467726553514c)](https://camo.githubusercontent.com/82cf9f43a6c1e27b3a99d64a5381684b23b10265a2c0ed0e3dec5ba95a5a0ebc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f706f7a6974726f6e696b2f796969322d6361636865642d70726f706572746965732d74726169742f434925323077697468253230506f737467726553514c)

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Run

```
php composer.phar require pozitronik/yii2-cached-properties-trait "^1.0.0"

```

or add

```
"pozitronik/yii2-cached-properties-trait": "^1.0.0"

```

to the require section of your `composer.json` file and run `composer.phar install`

Requirements
------------

[](#requirements)

PHP &gt;= 8.0

What is it?
-----------

[](#what-is-it)

let's imagine a situation: there is a Yii2 model with some long-to-compute property:

```
/**
 * @property ?int AnswerToUltimateQuestionOfLifeUniverseAndEverything
 */
class DeepThought extends \yii\base\Model {

	/**
	 * Warning: this method execution time take ~7.5 million years.
	 * @return int|null
	 */
	public function getAnswerToUltimateQuestionOfLifeUniverseAndEverything():?int {
		return $this->multiple(6, 9);//42
	}
}
```

Every time, when `$deepThoughtObject->AnswerToUltimateQuestionOfLifeUniverseAndEverything` called, we have to wait again. Of course, property value can be saved to a temporary variable after first call, or cached ( which is more prefferable, because caching is caching, you know), so next calls will cost nothing.

Usually, add caching support to you code in Yii2 is not so hard. Let's assume something like:

```
	/**
	 * Warning: the first execution of this method take ~7.5 million years
	 * @return int|null
	 */
	public function getAnswerToUltimateQuestionOfLifeUniverseAndEverything():?int {
		return Yii::$app->cache->getOrSet(
			'DeepThought::AnswerToUltimateQuestionOfLifeUniverseAndEverything',
			function() {
				return $this->multiple(6, 9);//42
			}
		);
	}
```

and it's OK. But what it that property have to recalculated somehow?

```
/**
 * @property mixed AnswerToUltimateQuestionOfLifeUniverseAndEverything
 * @property-write mixed $alpha
 * @property-write mixed $beta
 * @property-write mixed $base
 */
class DeepThought extends \yii\base\Model {
	private mixed $_alpha = 6;
	private mixed $_beta = 9;
	private mixed $_base = 13;

	/**
	 * Warning: this method execution take... idk, honestly
	 * @return mixed
	 */
	public function getAnswerToUltimateQuestionOfLifeUniverseAndEverything():mixed {
		return $this->multiple($this->_alpha, $this->_beta, $this->_base);//???
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setAlpha(mixed $value):void {
		$this->_alpha = $value;
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setBeta(mixed $value):void {
		$this->_beta = $value;
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setBase(mixed $value):void {
		$this->_base = $value;
	}

}

(new DeepThought())->AnswerToUltimateQuestionOfLifeUniverseAndEverything;//ok, 42?
(new DeepThought([
	'alpha' => 'cheese',
	'beta' => 'moon',
	'base' => 'infinity'
]))->AnswerToUltimateQuestionOfLifeUniverseAndEverything;//???

```

You have to watch for right cache invalidations and write a lot of boring code. This trait tries to simplify that task:

```
/**
 * @property mixed AnswerToUltimateQuestionOfLifeUniverseAndEverything
 * @property-write mixed $alpha
 * @property-write mixed $beta
 * @property-write mixed $base
 */
class DeepThought extends \yii\base\Model {
	use CachedPropertiesTrait;

    private mixed $_alpha = 6;
	private mixed $_beta = 9;
	private mixed $_base = 13;

	/**
	 * @inheritDoc
	 */
	public function cachedProperties():array {
		return [
			/* property cached, cache invalidates, when alpha/beta/base values are changed */
			['AnswerToUltimateQuestionOfLifeUniverseAndEverything', ['alpha', 'beta', 'base']]
		];
	}

	/**
	 * Warning: this method execution take ~7.5 million years
	 * @return mixed
	 */
	public function getAnswerToUltimateQuestionOfLifeUniverseAndEverything():mixed {
		return $this->multiple($this->_alpha, $this->_beta, $this->_base);//???
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setAlpha(mixed $value):void {
		$this->_alpha = $value;
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setBeta(mixed $value):void {
		$this->_beta = $value;
	}

	/**
	 * @param mixed $value
	 * @return void
	 */
	public function setBase(mixed $value):void {
		$this->_base = $value;
	}

}
```

So, that's an idea, see tests and code examples for more information.

License
-------

[](#license)

GNU GPL 3.0

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

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.

###  Release Activity

Cadence

Every ~76 days

Total

4

Last Release

1132d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/76ddc43524bb32eb36568e25b6ae283bc25bb51a4572789997909092145af0f5?d=identicon)[pozitronik](/maintainers/pozitronik)

---

Top Contributors

[![pozitronik](https://avatars.githubusercontent.com/u/2357892?v=4)](https://github.com/pozitronik "pozitronik (3 commits)")

---

Tags

cacheyii2extension

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pozitronik-yii2-cached-properties-trait/health.svg)

```
[![Health](https://phpackages.com/badges/pozitronik-yii2-cached-properties-trait/health.svg)](https://phpackages.com/packages/pozitronik-yii2-cached-properties-trait)
```

###  Alternatives

[undefinedor/yii2-cached-active-record

The cached activeRecord for the Yii2 framework

102.6k](/packages/undefinedor-yii2-cached-active-record)

PHPackages © 2026

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