承接 h4kuna/memoize 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

h4kuna/memoize

最新稳定版本:v0.1.7

Composer 安装命令:

composer require h4kuna/memoize

包简介

Provide cache to memory for your class.

README 文档

README

Downloads this Month Latest stable

Is one trait whose provide cache to memory for classes. This is abstract standard use case how cache data for one request. Example is below.

Api is easy where is one method memoize where first parameter is unique key string or array and second parameter is callback. This trait clear class.

Install by composer

$ composer require h4kuna/memoize

Standard use case

<?php

class Foo
{

	private $dataFromDatabase;

	private $users = [];


	public function loadDataFromDatabase()
	{
		if ($this->dataFromDatabase === null) {
			$this->dataFromDatabase = $this->repository->fetchAnyData();
		}
		return $this->dataFromDatabase;
	}


	public function loadDataFromDatabaseByUser($userId)
	{
		if (isset($this->users[$userId]) === false) {
			$this->users[$userId] = $this->repository->fetchUser($userId);
		}
		return $this->users[$userId];
	}

}

Use Memoize

<?php

class Foo
{
	use h4kuna\Memoize\Memoize;

	public function loadDataFromDatabase()
	{
		return $this->memoize(__METHOD__, function() {
			return $this->repository->fetchAnyData();
		});
	}


	public function loadDataFromDatabaseByUser($userId)
	{
		return $this->memoize([__METHOD__, $userId], function() use ($userId) {
			return $this->repository->fetchUser($userId);
		});
	}

}

Static use case

The similar class can be used for static class.

class Bar {
	use h4kuna\Memoize\MemoizeStatic

	public static function loadDataFromDatabaseByUser($userId)
	{
		return static::memoize([__METHOD__, $userId], function() use ($userId) {
			return User::fetchUser($userId);
		});
	}
}

Use both traits

This case is unlikely, so the names are the same. You can resolve by alias.

class Baz {
	use Memoize\Memoize, Memoize\MemoizeStatic {
		Memoize\Memoize::memoize insteadof Memoize\MemoizeStatic;
		Memoize\MemoizeStatic::memoize as memoizeStatic;
	}
	
	public function foo(): 
	{
		return $this->memoize();
	}
	
	public static function bar(): 
	{
		return static::memoizeStatic();
	}
}

Disable Memoize in tests

You can disable Memoize for tests in bootstrap.

use h4kuna\Memoize\Helper;
Helper::bypassMemoize();

统计信息

  • 总下载量: 136.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 3
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固