I’m creating a website that list events, each event have multiple tickets, when I’m listening events I need to show the minimum price and the number of tickets.
I have a function on Event model that return the minimum price, I want to cache the result since this query is called multiple times. This are my 2 approaches:
Data Cache with this solution I have about 25 queries per page
/**
* Return the minimum ticket price for this event
* @return integer
*/
public function getMinPrice()
{
$cache = Yii::$app->cache;
$minPrice = $cache->get('eventMinPrice_'.$this->id);
if($minPrice === false){
$minPrice = $this->hasMany(Ticket::className(), ['event_id' => 'id'])->where(['ticket.business_type' => Ticket::BUSINESS_TYPE_SELL, 'ticket.status' => Ticket::STATUS_ACTIVE, 'ticket.best_offer' => false])->min('price');
$cache->set('eventMinPrice_'.$this->id, $minPrice, 120);
}
return $minPrice;
}
Query cache with this solution I have about 55 queries per page:
/**
* Return the minimum ticket price for this event
* @return integer
*/
public function getMinPrice()
{
$db = Yii::$app->db;
$id = $this->id;
$result = $db->cache(function ($db) use ($id) {
return $this->hasMany(Ticket::className(), ['event_id' => 'id'])->where(['ticket.business_type' => Ticket::BUSINESS_TYPE_SELL, 'ticket.status' => Ticket::STATUS_ACTIVE, 'ticket.best_offer' => false])->min('price');
}, 60);
return $result;
}
Without cache I have about 123 queries per page.
Why the Query cache do more queries that the data cache? It seams that the best approach is a simple data cache, but I want your opinion.