{"id":33981072,"url":"https://github.com/laracraft-tech/memoize","last_synced_at":"2025-12-13T03:34:32.532Z","repository":{"id":62630112,"uuid":"561190772","full_name":"laracraft-tech/memoize","owner":"laracraft-tech","description":"Simple PHP trait to memoize function calls!","archived":false,"fork":false,"pushed_at":"2025-07-28T06:45:59.000Z","size":88,"stargazers_count":14,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-28T08:40:05.380Z","etag":null,"topics":["cache","memoization","performance","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/laracraft-tech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-11-03T06:33:29.000Z","updated_at":"2025-07-28T06:45:56.000Z","dependencies_parsed_at":"2025-05-12T06:22:07.666Z","dependency_job_id":"99b43af0-605b-473c-b45f-2f0f174ecb18","html_url":"https://github.com/laracraft-tech/memoize","commit_stats":{"total_commits":34,"total_committers":2,"mean_commits":17.0,"dds":0.08823529411764708,"last_synced_commit":"0b4900622ad471df1fe4a1b6c274fea6a0e9a132"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/laracraft-tech/memoize","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Fmemoize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Fmemoize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Fmemoize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Fmemoize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laracraft-tech","download_url":"https://codeload.github.com/laracraft-tech/memoize/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laracraft-tech%2Fmemoize/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27699634,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-13T02:00:09.769Z","response_time":147,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","memoization","performance","php"],"created_at":"2025-12-13T03:34:31.884Z","updated_at":"2025-12-13T03:34:32.527Z","avatar_url":"https://github.com/laracraft-tech.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Memoize\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/laracraft-tech/memoize.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/memoize)\n[![Tests](https://github.com/laracraft-tech/memoize/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/laracraft-tech/memoize/actions/workflows/run-tests.yml)\n[![Check \u0026 fix styling](https://github.com/laracraft-tech/memoize/actions/workflows/fix-php-code-style-issues.yml/badge.svg?branch=main)](https://github.com/laracraft-tech/memoize/actions/workflows/fix-php-code-style-issues.yml)\n[![License](https://img.shields.io/packagist/l/laracraft-tech/memoize.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/memoize)\n\u003c!--[![Total Downloads](https://img.shields.io/packagist/dt/laracraft-tech/memoize.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/memoize)--\u003e\n\n## Introduction\n\nThis package provides you with a simple PHP trait, which adds high-performance [memoization](https://en.wikipedia.org/wiki/Memoization) to your classes!\n\n## Installation\n\n``` bash\n$ composer require laracraft-tech/memoize\n```\n\n\n## Usage\n\nThe `memoize` method accepts a `callable`.\n\n```php\nuse LaracraftTech\\Memoize\\HasMemoization;\n\n$myClass = new class()\n{    \n    use HasMemoization;\n\n    public function getNumber(): int\n    {\n        return $this-\u003ememoize(function () {\n            return rand(1, 10000);\n        });\n    }\n};\n```\n\nNo matter how many times you run `$myClass-\u003egetNumber()` you'll always get the same number.\n\nThe `memoize` method will only run **once per combination** of `use` variables the ***closure*** receives.\n\n```php\n$myClass = new class()\n{\n    use HasMemoization;\n    \n    public function getNumberForLetter($letter)\n    {\n        return $this-\u003ememoize(function () use ($letter) {\n            return $letter . rand(1, 10000000);\n        });\n    }\n}\n```\n\nSo calling `$myClass-\u003egetNumberForLetter('A')` will always return the same result, but calling `$myClass-\u003egetNumberForLetter('B')` will return something else.\n\nSome memoization packages uses the arguments of the ***containing method*** for the **once per combination** idea.\nWe think this feels a bit unintuitive and in certain circumstances will affect performance. So we use the `use` variables of the closure as the **once per combination** key. As a fallback, if you like/need to, we also let you fully self define your **once per combination** key in a second optional parameter of the closure.\n\n```php\nuse LaracraftTech\\Memoize\\HasMemoization;\n\n$myClass = new class()\n{\n    use HasMemoization;\n    \n    public function processSomethingHeavy1($someModel)\n    {\n        // ...\n        \n        $relation = $this-\u003ememoize(function () use ($someModel) {\n            return Foo::find($someModel-\u003efoo_relation_id);\n        }, $someModel-\u003efoo_relation_id); // \u003c--- custom once per combination key\n        \n        // ...\n    }\n    \n    // you could also do something like this (maybe more convinient)\n    public function processSomethingHeavy2($someModel)\n    {\n        // ...\n        \n        $foo_relation_id = $someModel-\u003efoo_relation_id;\n        $relation = $this-\u003ememoize(function () use ($foo_relation_id) {\n            return Foo::find($foo_relation_id);\n        });\n        \n        // ...\n    }\n    \n    // this would work but will lose performance on each new $someModel even foo_relation_id would be the same\n    public function processSomethingHeavy3($someModel)\n    {\n        // ...\n        \n        $relation = $this-\u003ememoize(function () use ($someModel) {\n            return Foo::find($someModel-\u003efoo_relation_id);\n        });\n        \n        // ...\n    }\n}\n```\n\nSo when calling `$myClass-\u003eprocessSomethingHeavy1(SomeModel::find(1))` the variable `$relation` will always have the same value like in `$myClass-\u003eprocessSomethingHeavy1(SomeModel::find(2))` as long as they have the same `foo_relation_id`. In some other memoization packges you would lose performance here, cause the ***containing method*** parameter ($someModel) has changed... Note that `processSomethingHeavy3` would also lose performance, even though the `foo_relation_id` would be the same, cause here also the changed **$someModel** would be used as the combination key and that model would have at least a different id.\n\n## Enable/Disable\n\nYou can globally enable or disable the memoize cache by setting `MEMOIZATION_GLOBALLY_DISABLED` to `true` in your `.env` file.\n\nIf you only want to disable to memoize cache for a specifiy class, do something like this:\n\n```php\nuse LaracraftTech\\Memoize\\HasMemoization;\n\nclass MyClass\n{\n    use HasMemoization;\n    \n    public function __construct()\n    {\n        $this-\u003edisableMemoization();\n    }\n}\n```\n\n## Change log\n\nPlease see the [changelog](changelog.md) for more information on what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Contributing\n\nPlease see [contributing.md](contributing.md) for details and a todolist.\n\n## Security\n\nIf you discover any security related issues, please email zacharias.creutznacher@gmail.com instead of using the issue tracker.\n\n## Credits\n\n- [Zacharias Creutznacher][link-author]\n- [All Contributors][link-contributors]\n\n## License\n\nMIT. Please see the [license file](license.md) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/laracraft-tech/laravel-dynamic-model.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/laracraft-tech/laravel-dynamic-model.svg?style=flat-square\n[ico-travis]: https://img.shields.io/travis/laracraft-tech/laravel-dynamic-model/master.svg?style=flat-square\n[ico-styleci]: https://styleci.io/repos/12345678/shield\n\n[link-packagist]: https://packagist.org/packages/laracraft-tech/laravel-dynamic-model\n[link-downloads]: https://packagist.org/packages/laracraft-tech/laravel-dynamic-model\n[link-travis]: https://travis-ci.org/laracraft-tech/laravel-dynamic-model\n[link-styleci]: https://styleci.io/repos/12345678\n[link-author]: https://github.com/laracraft-tech\n[link-contributors]: ../../contributors\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaracraft-tech%2Fmemoize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaracraft-tech%2Fmemoize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaracraft-tech%2Fmemoize/lists"}