Y
Y
y0rker2015-10-22 12:35:45
Yii
y0rker, 2015-10-22 12:35:45

How to delete fragment cache in yii2?

In 1x the code does not work stupidly

$dependency = [
    'class' => 'yii\caching\DbDependency',
    'sql' => "SELECT updated_at FROM fm_competitions_matches WHERE id = {$round->id}"
];
if ($this->beginCache('Round'.$round->id, [
    'duration' => 3600,
    'dependency'=> $dependency,
])) {
    // Кешируемый код
    $this->endCache();
}

And when updated_at is changed in the database, the cache is not updated.
Clearing the cache doesn't work either.
Yii::$app->cache->delete('Round'.$round->id);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
y0rker, 2015-10-22
@y0rker

I shoveled the framework code and found that the key for fragments is generated differently.
Solving the problem with clearing the fragment cache

$buldKey = ['yii\widgets\FragmentCache', 'Round'.$round->id];
Yii::$app->cache->delete($buldKey);

A
Alexander Makarov, 2015-10-22
@SamDark

Why the dependence does not work immediately I do not know. It is not cleaned because the cache key for fragments is calculated automatically to prevent conflicts, like this:
https://github.com/yiisoft/yii2/blob/master/framew...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question