Ouzo\Utilities\Suppliers::memoize PHP Метод

memoize() публичный статический Метод

Returns a supplier which caches the callback result and returns that value on subsequent calls to get().
public static memoize ( callable $function ) : Ouzo\Utilities\Supplier\Supplier
$function callable
Результат Ouzo\Utilities\Supplier\Supplier
    public static function memoize($function)
    {
        return new MemoizingSupplier($function);
    }

Usage Example

Пример #1
0
 /**
  * @test
  */
 public function memoizeShouldCacheResult()
 {
     //given
     $command = Mock::mock();
     Mock::when($command)->getName()->thenReturn('Jack', 'Black', 'White');
     $supplier = Suppliers::memoize(function () use($command) {
         return $command->getName();
     });
     //when
     Clock::freeze('2014-01-01 11:11:11');
     $result1 = $supplier->get();
     $result2 = $supplier->get();
     Clock::freeze('2014-01-01 11:11:12');
     $result3 = $supplier->get();
     Clock::freeze('2014-01-01 11:11:22');
     $result4 = $supplier->get();
     $result5 = $supplier->get();
     //then
     $this->assertEquals('Jack', $result1);
     $this->assertEquals('Jack', $result2);
     $this->assertEquals('Jack', $result3);
     $this->assertEquals('Jack', $result4);
     $this->assertEquals('Jack', $result5);
 }