PHP code example of zeus / memoize

1. Go to this page and download the library: Download zeus/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/ */

    

zeus / memoize example snippets



use function Zeus\Memoize\once;

$closure=function (){
 sleep(1);
 return random_int(1,100);
}

echo once($closure); // same value (it will sleep)
echo once($closure); //same value, it tooks from cache 



use function Zeus\Memoize\once;


 $http = new  Http();
 
 $closure = function () use ($http) {
     return $http->get('https://www.foobar.com');
 };
 
 $content = once($closure);
 $content1=once($closure);
 
 //$content===$content1 =>true




use function Zeus\Memoize\once;


 $linkedin = new  Linkedin();
 $linkedin->setName('dılo sürücü');
 
 $closure = function () use (&$linkedin) {
     return $http->getProfiles();
 };
 
$content = once($closure);
$linkedin->setName('abdulkadir sürücü');
$content1=once($closure);
 
 //$content===$content1 =>false


use Zeus\Memoize\OnceWrapper;


class Random
{

    /**
     * @throws Exception
     */
    public function getInteger(int $start, int $end):int
    {
        return random_int($start, $end);
    }
}

 $random = new Random();
 //let's change of object instance of variable with @phpdoc @var
 /**
 * @var Random $once 
 */
 $once = new OnceWrapper($random);

 $first = $once->getInteger(1, 100);
 $second = $once->getInteger(1, 100);
 $third = $once->getInteger(200, 300);
 
 //$second===$third =>false
 
 $this->assertEquals($first, $second);





class Random
{

    /**
     * @throws Exception
     */
    public function getInteger(int $start, int $end):int
    {
        return random_int($start, $end);
    }
}

$random = new Random();
 //let's change of object instance of variable with @phpdoc @var
 /**
 * @var Random $once 
 */
$once = new OnceWrapper($random);
$once->setCacheAdapter(new RedisCacheAdapter());

$first = $once->getInteger(1, 100);
$second = $once->getInteger(1, 100); 


use function Zeus\Memoize\once_wrapper;

class Random
{

    /**
     * @throws Exception
     */
    public function getInteger(int $start, int $end):int
    {
        return random_int($start, $end);
    }
}

$first = once_wrapper(new Random())->getInteger(1, 100);
$second = once_wrapper(new Random())->getInteger(1, 100);

var_dump($first===$second); //true