PHP code example of zheltikov / php-memoize

1. Go to this page and download the library: Download zheltikov/php-memoize library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zheltikov / php-memoize example snippets




unction Zheltikov\Memoize\wrap;

function my_expensive_function(string $who): string
{
    // Just for illustrative purposes we just sleep here,
    // but here could be a database fetch, or an expensive
    // computation.
    sleep(5);
    return 'Hello ' . $who . '!';
}

my_expensive_function('World'); // Runs in 5 seconds

$memoized = wrap('my_expensive_function');

$memoized('World'); // 5 seconds
$memoized('World'); // milliseconds

$memoized('Everyone'); // 5 seconds
$memoized('Everyone'); // milliseconds




heltikov\Memoize\{Config, DefaultKeyGenerator};
use function Zheltikov\Memoize\wrap;

// You can change the hashing algorithm for a specific function like so:
$wrapped = wrap(function () { /* ... */ });
$wrapped->getKeyGenerator()->setHashAlgo('whirlpool');

// Or you can change it for all previously created functions:
Config::setKeyGenerators(
    (new DefaultKeyGenerator())
        ->setHashAlgo('sha256')
);

shell
$ composer