Reputation: 31
I need help with caching Active Record data in Yii2.
In documentation has example:
$result = Customer::getDb()->cache(function ($db) {
return Customer::find()->where(['id' => 1])->one();
});
I understand that it need to implement the method in model class such a:
public static function getByPk($pk)
{
$result = self::getDb()->cache(function ($db) use ($pk) {
return self::find()->where(['id' => $pk])->one();
});
return $result;
}
If this query result was already cached in $result
will set value from cache, otherwise will execute query, correctly?
Also I have question about dependency, what dependency I can use for invalidation this cache value?
Upvotes: 1
Views: 449
Reputation: 4560
As an opinion! You know it depends on data that you add to cache storage. If it's some static data such as status, position etc. you can set the second param duration
. You can read about at official doc.
If you make a few same queries on the page you can set 1 or 2 seconds and it helps you a little.
Upvotes: 0