drupol/memoize

Memoize a callable.

Fund package maintenance!
drupol

3.0.0 2020-01-28 18:07 UTC

This package is auto-updated.

Last update: 2020-09-28 19:49:44 UTC


README

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Code Coverage Type Coverage License Donate!

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.

It can use any type of Cache backend system, as long as it implements the PSR-6 cache interface.

If you use the symfony/cache package, you will have a bunch of cache backends available such as Redis, MemCache, Filesystem, ArrayCache,...

Features

  • Provides a Memoizer class,
  • Allows you to set a custom cache,

Installation

With composer:

composer require drupol/memoize

Usage

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use drupol\memoize\Memoizer;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$callback = function ($a = 5, $b = 10) {
    sleep(5);

    return \sprintf('Param 1: %s  Param 2: %s%s', $a, $b, \PHP_EOL);
};

$memoizer = new Memoizer($callback, 'unique ID');

echo $memoizer('A', 'B') . "\n";
echo $memoizer('C', 'D') . "\n";
echo $memoizer('A', 'B') . "\n";
echo $memoizer('C', 'D') . "\n";

By default, no cache object is provided and it uses simple PHP arrays. However, if you want to use a custom cache, the ArrayAccessCacheItemPool class is made for you.

Do composer require symfony/cache or any other PSR-6 compliant library, then:

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use drupol\memoize\Cache\ArrayAccessCacheItemPool;
use drupol\memoize\Memoizer;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

// Decorate the FilesystemAdapter() cache.
$cache = new ArrayAccessCacheItemPool(new FilesystemAdapter());

$callback = function ($a = 5, $b = 10) {
    sleep(5);

    return \sprintf('Param 1: %s  Param 2: %s%s', $a, $b, \PHP_EOL);
};

$memoizer = new Memoizer($callback, 'unique ID', $cache);

echo $memoizer('A', 'B') . "\n";
echo $memoizer('C', 'D') . "\n";
echo $memoizer('A', 'B') . "\n";
echo $memoizer('C', 'D') . "\n";

The ArrayAccessCacheItemPool class is a decorator for PSR-6 CacheItemPoolInterface objects. It implements at the same time the core ArrayAccess interface and CacheItemPoolInterface interface. Once decorated, your PSR-6 cache will behave just like a simple PHP array.

Code style, code quality, tests and benchmarks

The code style is following PSR-12 plus a set of custom rules, the package drupol/php-conventions is responsible for this.

Every time changes are introduced into the library, Github CI run the tests and the benchmarks.

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests.

PHPInfection is used to ensure that your code is properly tested, run composer infection to test your code.

Contributing

See the file CONTRIBUTING.md but feel free to contribute to this library by sending Github pull requests.