framework/caching/Cache.php
public function getOrSet($key, $callable, $duration = null, $dependency = null)
    {
        if (($value = $this->get($key)) !== false) {
            return $value;
        }

        $value = call_user_func($callable, $this);
        if (!$this->set($key, $value, $duration, $dependency)) {
            Yii::warning('Failed to set cache value for key ' . json_encode($key), __METHOD__);
        }

        return $value;
    }
public function getOrSet($key, $callable, $duration = null, $dependency = null)
    {
        if (($value = $this->get($key)) !== false) {
            return $value;
        }

        $value = call_user_func($callable, $this);
        if (!$this->set($key, $value, $duration, $dependency)) {
            Yii::warning('Failed to set cache value for key ' . json_encode($key), __METHOD__);
        }

        return $value;
    }
Method combines both [[set()]] and [[get()]] methods to retrieve value identified by a $key,
or to store the result of $callable execution if there is no cache available for the $key.

Usage example:

```php
public function getTopProducts($count = 10) {
$cache = $this->cache; // Could be Yii::$app->cache
return $cache->getOrSet(['top-n-products', 'n' => $count], function () use ($count) {
return Products::find()->mostPopular()->limit($count)->all();
}, 1000);
}
```

@param mixed $key a key identifying the value to be cached. This can be a simple string or
a complex data structure consisting of factors representing the key.
@param callable|\Closure $callable the callable or closure that will be used to generate a value to be cached.
If you use $callable that can return `false`, then keep in mind that [[getOrSet()]] may work inefficiently
because the [[yii\caching\Cache::get()]] method uses `false` return value to indicate the data item is not found
in the cache. Thus, caching of `false` value will lead to unnecessary internal calls.
@param int $duration default duration in seconds before the cache will expire. If not set,
[[defaultDuration]] value will be used.
@param Dependency $dependency dependency of the cached item. If the dependency changes,
the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
This parameter is ignored if [[serializer]] is `false`.
@return mixed result of $callable execution
@since 2.0.11