定制 pozitronik/yii2-cached-properties-trait 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pozitronik/yii2-cached-properties-trait

最新稳定版本:1.0.3

Composer 安装命令:

composer require pozitronik/yii2-cached-properties-trait

包简介

Support to cache yii2 models properties

README 文档

README

Support to cache yii2 models properties

GitHub Workflow Status

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

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2022-08-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固