Ouzo\Utilities\Cache::memoize PHP Method

memoize() public static method

Example: $countries = Cache::memoize(function() {{ expensive computation that returns a list of countries return Country:all(); })
public static memoize ( $function ) : mixed | null
$function
return mixed | null
    public static function memoize($function)
    {
        $reflectionFunction = new ReflectionFunction($function);
        $key = "{$reflectionFunction->getFileName()}:{$reflectionFunction->getEndLine()}";
        return self::get($key, $function);
    }

Usage Example

Example #1
0
 /**
  * @test
  */
 public function shouldUseDifferentKeysForDifferentClosures()
 {
     //when
     $result1 = Cache::memoize(function () {
         return 1;
     });
     $result2 = Cache::memoize(function () {
         return 2;
     });
     //then
     $this->assertEquals(2, Cache::size());
     $this->assertEquals(1, $result1);
     $this->assertEquals(2, $result2);
 }