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 | |
return | Ouzo\Utilities\Supplier\Supplier |
/** * @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); }