zeus/memoize

php memoize library

v1.0.0 2022-10-27 08:36 UTC

This package is auto-updated.

Last update: 2024-10-23 15:50:15 UTC


README

Php memoize

It does not make different logic each time, it does it in one go and stores the result in memory, so there is no extra operation and time loss when it is called repeatedly, it returns the previous result

for install with composer

composer require  zeus/memoize

for test

composer test

Basic usage

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 

Parameter usage

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

it sensitive to object change
Via the & operator

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

once wrappers for objects
OnceWrapper object offers memoize for object,this object is sensitive to change of parameters
usage

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);

do you notice something, getInteger() method Random object in method, but it's like the method of OnceWrapper object, yes it works just like that

Custom cache Adapter
you can use custom cache adapter for customizable cache like redis or memcache .Assuming you have a class called ** RedisCacheAdapter**,you can use the "setCacheAdapter" method for this,...

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); 

once_wrapper global helper
useful global function to memoize objects, also added generic class for phpstorm auto complete...

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