yii-starter-kit / yii2-starter-kit

Yii2 Starter Kit
http://yii2-starter-kit.terentev.net
Other
1.42k stars 648 forks source link

Use new cache function `getOrSet` to improve readability in components #703

Closed XzAeRo closed 4 years ago

XzAeRo commented 5 years ago

Example:

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);
}
XzAeRo commented 4 years ago

Not implementing this since DB queries are automatically cached. Might enable it again for some other more complex examples.