定制 zenstruck/memoize 二次开发

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

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

zenstruck/memoize

最新稳定版本:v1.0.0

Composer 安装命令:

composer require zenstruck/memoize

包简介

Helper trait to efficiently cache expensive methods in memory.

关键字:

README 文档

README

CI codecov

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 WeakMap keyed by each object's instance. They are automatically cleared as the objects are garbage collected.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-26