定制 kenny1911/php-memoizer 二次开发

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

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

kenny1911/php-memoizer

最新稳定版本:0.1.0

Composer 安装命令:

composer require kenny1911/php-memoizer

包简介

README 文档

README

[English][Русский]

A library for memoizing function results in PHP. Allows caching function and method call results for performance optimization.

Installation

composer require kenny1911/php-memoizer

Quick Start

Initialization

use Kenny1911\Memoizer\Cache\InMemoryCache;
use Kenny1911\Memoizer\Memoizer;
use Kenny1911\Memoizer\Normalizer\SimpleNormalizer;

$memoizer = new Memoizer(
    normalizer: new SimpleNormalizer(),
    cache: new InMemoryCache(),
);

Usage in Class

final readonly class Calculator
{
    public function __construct(
        private Memoizer $memoizer,
    ) {}

    public function sum(int $a, int $b): int
    {
        return $this->memoizer->memoize([__METHOD__, \func_get_args()], function() use ($a, $b): int {
            return $a + $b;
        });
    }
}

$calculator = new Calculator($memoizer);

$calculator->sum(1, 2); // Calculates value: 3
$calculator->sum(1, 2); // Returns memoized value: 3

$calculator->sum(3, 4); // Calculates value: 7

Components

Memoizer

The main class for memoization. Accepts two parameters:

  • normalizer - normalizer for creating cache keys
  • cache - cache implementation

Normalizers

SimpleNormalizer

Converts arguments to a string key using the standard PHP serialize() function. Suitable for most cases.

Cache

InMemoryCache

Stores data in memory during script execution.

Customization

To customize the memoizer, you can write and use your own implementation of a normalizer (interface Kenny1911\Memoizer\Normalizer\Normalizer) and/or cache (interface Kenny1911\Memoizer\Cache\Cache).

Recommendations

  1. Use unique keys - include the method name and all relevant parameters in the key
  2. Avoid memoizing side effects - memoization is only suitable for pure functions
  3. Consider memory usage - especially when using InMemoryCache
  4. Test performance - make sure memoization actually provides benefits

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固