An open API service indexing awesome lists of open source software.

https://github.com/loophp/memoize

Memoize a closure.
https://github.com/loophp/memoize

cache educational memoization memoize memoizer

Last synced: 4 months ago
JSON representation

Memoize a closure.

Awesome Lists containing this project

README

          

[![Latest Stable Version][latest stable version]][packagist]
[![GitHub stars][github stars]][packagist]
[![Total Downloads][total downloads]][packagist]
[![GitHub Workflow Status][github workflow status]][github actions]
[![Scrutinizer code quality][code quality]][code quality link]
[![Type Coverage][type coverage]][sheperd type coverage]
[![Code Coverage][code coverage]][code quality link]
[![License][license]][packagist]
[![Donate!][donate github]][github sponsor]
[![Donate!][donate paypal]][paypal sponsor]

# PHP Memoize

## Description

Memoizer class for callable.

From wikipedia:
> In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

This library help you to memoize callable or closures.

## Features

* Provides a Memoizer class.
* Immutable.
* Stateless.

## Installation

With composer:

`composer require loophp/memoize`

## Usage

```php
= $number) ?
$number :
$fibonacci($number - 1) + $fibonacci($number - 2);
};

$fibonacciMemoized = static function (int $number) use (&$fibonacciMemoized): int {
return (1 >= $number) ?
$number :
$fibonacciMemoized($number - 1) + $fibonacciMemoized($number - 2);
};
$fibonacciMemoized = Memoizer::fromClosure($fibonacciMemoized);

function bench(Closure $closure, ...$arguments): array
{
$eval = static function (Closure $closure, ...$arguments): Generator {
yield microtime(true);
yield $closure(...$arguments);
yield microtime(true);
};

$result = iterator_to_array($eval($closure, ...$arguments));

return [
$result[1],
$result[2] - $result[0],
];
}

var_dump(sprintf('[return: %s] [duration: %s]', ...bench($fibonacci, 30))); // ~3 seconds
var_dump(sprintf('[return: %s] [duration: %s]', ...bench($fibonacciMemoized, 30))); // ~0.0003 seconds
```

## Code style, code quality, tests and benchmarks

The code style is following [PSR-12](https://www.php-fig.org/psr/psr-12/) plus a set of custom rules, the package [drupol/php-conventions](https://github.com/drupol/php-conventions)
is responsible for this.

Every time changes are introduced into the library, [Github CI](https://github.com/drupol/memoize/actions) run the tests and the benchmarks.

The library has tests written with [PHPSpec](http://www.phpspec.net/).
Feel free to check them out in the `spec` directory. Run `composer phpspec` to trigger the tests.

[PHPInfection](https://github.com/infection/infection) is used to ensure that your code is properly tested, run `composer infection` to test your code.

## Contributing

See the file [CONTRIBUTING.md](.github/CONTRIBUTING.md) but feel free to contribute to this library by sending Github pull requests.

[latest stable version]: https://img.shields.io/packagist/v/loophp/memoize.svg?style=flat-square
[packagist]: https://packagist.org/packages/loophp/memoize

[github stars]: https://img.shields.io/github/stars/loophp/memoize.svg?style=flat-square

[total downloads]: https://img.shields.io/packagist/dt/loophp/memoize.svg?style=flat-square

[github workflow status]: https://img.shields.io/github/workflow/status/loophp/memoize/Continuous%20Integration?style=flat-square
[github actions]: https://github.com/loophp/memoize/actions

[code quality]: https://img.shields.io/scrutinizer/quality/g/loophp/memoize/master.svg?style=flat-square
[code quality link]: https://scrutinizer-ci.com/g/loophp/memoize/?branch=master

[type coverage]: https://shepherd.dev/github/loophp/memoize/coverage.svg
[sheperd type coverage]: https://shepherd.dev/github/loophp/memoize

[code coverage]: https://img.shields.io/scrutinizer/coverage/g/loophp/memoize/master.svg?style=flat-square
[code quality link]: https://img.shields.io/scrutinizer/quality/g/loophp/memoize/master.svg?style=flat-square

[license]: https://img.shields.io/packagist/l/loophp/memoize.svg?style=flat-square

[donate github]: https://img.shields.io/badge/Sponsor-Github-brightgreen.svg?style=flat-square
[github sponsor]: https://github.com/sponsors/drupol

[donate paypal]: https://img.shields.io/badge/Sponsor-Paypal-brightgreen.svg?style=flat-square
[paypal sponsor]: https://www.paypal.me/drupol

[phpspec]: http://www.phpspec.net/
[grumphp]: https://github.com/phpro/grumphp
[infection]: https://github.com/infection/infection
[phpstan]: https://github.com/phpstan/phpstan
[psalm]: https://github.com/vimeo/psalm
[changelog-md]: https://github.com/loophp/memoize/blob/master/CHANGELOG.md
[git-commits]: https://github.com/loophp/memoize/commits/master
[changelog-releases]: https://github.com/loophp/memoize/releases