zenstruck/memoize
最新稳定版本:v1.0.0
Composer 安装命令:
composer require zenstruck/memoize
包简介
Helper trait to efficiently cache expensive methods in memory.
关键字:
README 文档
README
Helper trait to efficiently cache expensive methods in memory.
Installation
composer require zenstruck/memoize
Usage
Add the memoize trait to an object you wish to cache operations on.
use Zenstruck\Memoize; class MyObject { use Memoize; public function method1(): mixed { // cache key defaults to the method name "method1" return $this->memoize( fn() => $this->someExpensiveOperation() // called only the first time method1() is called ); } public function method2(): mixed { return $this->memoize( fn() => $this->someExpensiveOperation(), 'my_custom_cache_key' // explicitly set the cache key ); } public function method3(string $parameter): mixed { return $this->memoize( fn() => $this->someExpensiveOperation($parameter) // called once per unique parameter 'my_custom_cache_key'.$parameter, // cache key includes the parameter ) } public function refresh(): void { $this->clearMemoized(); // clear all cached values for this object instance $this->clearMemoized('method1'); // clear just the cached value for "method1" } }
Note: The cached values are stored in a
WeakMapkeyed by each object's instance. They are automatically cleared as the objects are garbage collected.
统计信息
- 总下载量: 80
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-26