Hi all,
I am trying to figure out how I can set query cache to expire ( or update ) when value of some field in database change.
So if I am selecting number of employees that each company has, and I cache that query, I would like to update result returned by cache if number of employees change for any company.
Here is the table:
Company name - employees
Yii soft - 10
Trance soft - 20
Query that is cached will return the result of 30 when I do sum of all employees. But, if in Yii soft company, number employees change to 20, cached query should return 40. How can I achieve this, knowing that there is no update time field in this table ?
Here is my real query:
$duration = 60;
$dependency = [
'class' => 'yii\caching\DbDependency',
'sql' => "<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />",
];
$result = $db->cache(function ($db) {
return $db->createCommand("SELECT floor(sum(variable_value)) as totalSum
FROM company_report
JOIN company ON company_report.company_number = company.company_number
JOIN users ON company.id = users.company_id
JOIN club ON users.id = club.users_id
JOIN club_offer ON club.id = club_offer.club_id
JOIN club_offer_cpv ON club_offer.id = club_offer_id
WHERE club_offer_cpv.cpv_id LIKE '03%'
AND company_report.variable_code = 'AOP 201'
AND company_report.report_year = 2013
")->queryAll();
}, $duration, $dependency);
return $result;
Fields in real table are: id, company_name, report_year, variable_code, variable_value.
As you can see, I have tried using duration dependency, thinking that after duration of cache expire, cache mechanism will get new data and insert it into file where cached data is stored. But this is not happening. After duration expire, and user visit the same page again, he will get old cached data.
So, if values in database change, I want my cache to change too. How can I do this ? Thanks