How to flush db query cache in yii2?
Asked Answered
M

3

8

How to handle it when some changes happen in specific table records ?

public static function getAirportWithCache($iata_code){

              $result = Airports::getDb()->cache(function ($db) use ($iata_code){
                     $res = Airports::find()
                               ->where(['iata_code' => $iata_code])
                               ->limit(1)
                               ->asArray()
                               ->one();
                     return $res;
              });
              return $result;
        }
Mazur answered 8/2, 2016 at 12:19 Comment(13)
Use Delete().Ives
In db query i don't have specific key. Like Yii::$app->cache->delete($key); In my example how to do it?Mazur
set specific key for cache then call delete(). Use set()Ives
Try: Yii::$app->cache->set('dbCache', $result, 0, null)Ives
No. it is not working. As suggested by @SohelAhmedM it will work but it will remove all cache data.Mazur
Not getting any error.Mazur
otherwise, you have to flush all cache.Ives
Yes. i think this will be final solution.Mazur
nope, may be still there is also some workaround for this, you should stay posted.Ives
Okay. i have also raised this issue on yii2 forumMazur
You should simply use cache dependency : yiiframework.com/doc-2.0/…Stercoraceous
@Stercoraceous i don't have key my scenario.Mazur
You don't need key, just dependency...Stercoraceous
I
14

For global cache, can use :

Yii::$app->cache->flush();

for spesific you can use TagDependency :

 //way to use
return Yii::$app->db->cache(function(){
    $query =  new Query();
    return $query->all();
}, 0, new TagDependency(['tags'=>'cache_table_1']));

//way to flush when modify table
TagDependency::invalidate(Yii::$app->cache, 'cache_table_1');

see the documentation http://www.yiiframework.com/doc-2.0/yii-caching-tagdependency.html

Interdepartmental answered 4/9, 2016 at 6:59 Comment(0)
S
4

You should simply use \yii\caching\DbDependency, e.g. :

public static function getAirportWithCache($iata_code){
    // for example if airports count change, this will update your cache
    $dependency = new \yii\caching\DbDependency(['sql' => 'SELECT COUNT(*) FROM ' . Airports::tableName()]);
    $result = Airports::getDb()->cache(function ($db) use ($iata_code){
        return Airports::find()
            ->where(['iata_code' => $iata_code])
            ->limit(1)
            ->asArray()
            ->one();
    }, 0, $dependency);
    return $result;
}

Read more...

Stercoraceous answered 8/2, 2016 at 15:0 Comment(2)
This should be accepted as the answer as it is the correct solution according to yii2-specs!Used
I thought of doing this but how does Yii know when the data has changed? To know wouldn't it have to do a query to find out and hence defeat the purpose of caching in the first place!?Howse
P
2

Just execute Yii::$app->cache->flush(); anywhere (probably in your controller)

PS: It will delete entire cache stored in your server

Philadelphia answered 8/2, 2016 at 12:25 Comment(3)
I will check. and how to handle if any changes happen in table specific record?Mazur
Also this will remove my whole caching. i want to specific for db queries.Mazur
This definitely is not the solution, as it will flush your whole cache. This means query-cache, rbac-cache, schema-cache about everything except if you had multiple cache-components attached...Used

© 2022 - 2024 — McMap. All rights reserved.