Ouzo\Utilities\Cache::get PHP Méthode

get() public static méthode

If there's no object for the given key and $functions is passed, $function result will be stored in cache under the given key. Example: $countries = Cache::get("countries", function() {{ expensive computation that returns a list of countries return Country:all(); })
public static get ( $key, null $function = null ) : mixed | null
$key
$function null
Résultat mixed | null
    public static function get($key, $function = null)
    {
        if (!self::contains($key) && $function) {
            self::put($key, call_user_func($function));
        }
        return Arrays::getValue(self::$_cache, $key);
    }

Usage Example

Exemple #1
0
 /**
  * @test
  */
 public function shouldCacheNullValues()
 {
     //given
     $function = function () {
         ++CacheTest::$call_count;
         return null;
     };
     //when
     $result1 = Cache::get("id", $function);
     $result2 = Cache::get("id", $function);
     //then
     $this->assertEquals(1, CacheTest::$call_count);
     $this->assertNull($result1);
     $this->assertNull($result2);
 }