Answer the question
In order to leave comments, you need to log in
Why is Yii2 not finding the cache?
There is an application. At certain moments, a cache of some selection from the database is created:
public static function getAllHosts($by = 'id')
{
$_cache = Yii::$app->cache;
$key_cache_list_hosts = 'list_hosts_array_' . $by;
$dependency = new \yii\caching\DbDependency(['sql' => 'SELECT COUNT(*) FROM {{%hosts}}']);
$_hosts = $_cache->get($key_cache_list_hosts);
if ($_hosts === false) {
try {
$_hosts = static::find()->asArray()->indexBy($by)->all();
} catch (\Exception $e) {
echo "\n\nFatal error in function <Hosts/getAllHosts>!\n{$e->getMessage()}\nEnd...\n";
exit;
}
$_cache->set($key_cache_list_hosts, $_hosts, 3600 * 12, $dependency);
}
return $_hosts;
}
public static function deleteAllKeyHosts()
{
$_cache = Yii::$app->cache;
$key_cache = [];
$pref = 'list_hosts_array_';
$key_cache[] = $pref . 'id';
$key_cache[] = $pref . 'name';
$key_cache[] = $pref . 'comment';
foreach ($key_cache as $_key) {
$_cache->delete($_key);
}
}
Answer the question
In order to leave comments, you need to log in
The question is closed! After a long study of the problem, I found out that the cant is mine - the application had a module that used a separate config, and in it the application id was different from the "main" one...
Based on your code, this is quite possible
. You are deleting the cache with three keys, one of them may not be. Add an existence check before deletion.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question