pozitronik / yii2-cached-properties-trait
Support to cache yii2 models properties
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=8.0
- pozitronik/yii2-helpers: ^1.0.13
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- codeception/codeception: ^4.1
- codeception/module-asserts: ^1.3
- codeception/module-yii2: ^1.1
- phpunit/phpunit: ^9.5
- pozitronik/yii2-references: ^1.2.0
- yiisoft/yii2-bootstrap4: ~2.0.10
This package is auto-updated.
Last update: 2024-11-07 10:30:44 UTC
README
Support to cache yii2 models properties
Installation
The preferred way to install this extension is through composer.
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
PHP >= 8.0
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
GNU GPL 3.0