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

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

Subsequent calls to get() return the cached value if expiration time has not passed. Time is passed in seconds.
public static memoizeWithExpiration ( callable $function, integer $expireTime = 3600 ) : Ouzo\Utilities\Supplier\Supplier
$function callable
$expireTime integer
Результат Ouzo\Utilities\Supplier\Supplier
    public static function memoizeWithExpiration($function, $expireTime = 3600)
    {
        return new ExpiringMemoizingSupplier($function, $expireTime);
    }

Usage Example

Пример #1
0
 /**
  * @test
  */
 public function memoizeWithExpirationShouldCacheResultForGivenTime()
 {
     //given
     $command = Mock::mock();
     Mock::when($command)->getName()->thenReturn('Jack', 'Black', 'White');
     $supplier = Suppliers::memoizeWithExpiration(function () use($command) {
         return $command->getName();
     }, 10);
     //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('Black', $result4);
     $this->assertEquals('Black', $result5);
 }