koktut/php-memoize

Memoization functions result

This package's canonical repository appears to be gone and the package has been frozen as a result. Email us for help if needed.

Installs: 456

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 2

Forks: 0

pkg:composer/koktut/php-memoize

dev-master 2020-10-09 09:59 UTC

This package is auto-updated.

Last update: 2024-03-09 17:01:02 UTC


README

Implementation of memoization on PHP

Code Climate Issue Count Test Coverage

Install

composer require koktut/php-memoize

Usage

use \PhpMemo\MemoryCache;
use \PhpMemo\DiskCache;

$slowFunction = function ($val1, $val2) {
    // do something slow
    sleep(1);

    return $val1 + $val2;
};

$memo = new MemoryCache();              // or $memo = new DiskCache(sys_get_temp_dir());
$func= $memo->memoize($slowFunction);

// first call
echo $func(1, 2) . ' ' . sprintf("%f\n", $memo->getLastOpTime());   // 3 1.000000

// second call
echo $func(1, 2) . ' ' . sprintf("%f\n", $memo->getLastOpTime());   // 3 0.000000