c01l/phpdecorator-memo
Composer 安装命令:
composer require c01l/phpdecorator-memo
包简介
Memoization for PHP methods using a Python style Decorator pattern.
README 文档
README
The phpdecorator library extension can be used to store return values in memory depending on its parameters.
This library uses the parameters of a function to build a cache key to cache function call results.
How to use it?
- Add
Memoattribute to a function. - Specify the how the cache key is built.
class TestClass { #[Memo(["bar"])] public function foo(int $bar, int $x): int { return $x; } } $obj = new TestClass(); $obj = (new \C01l\PhpDecorator\DecoratorManager())->decorate($obj); $obj->foo(1,2) // 2 $obj->foo(2,3) // 3 $obj->foo(1,3) // 2, because foo is not really executed again, but the cached version is used
Using complex memo keys
The key can be composed of multiple values, by adding more elements to the array:
#[Memo(["bar", "x"])] public function foo($bar, $x) { return $x; }
Using no field will result in the function being only called once and different arguments are not considered.
#[Memo] public function foo($bar, $x) { return $x; } ... $obj->foo(1,2); // 2 $obj->foo(1,3); // 2 $obj->foo(2,5); // 2 $obj->foo(4,6); // 2
You can use an arbitrary expression in the key definition.
class A { public int $x, $y, $z; public function getX() { return $x; } } ... #[Memo(["bar->z", "bar->getX()"])] public function foo(A $bar, $x) { return $x; }
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-30