我使用的是yii的高级版本, 现在我需要在后台清理前端页面缓存, 在yii中有一个方法:
Yii::$app->cache->flush();
但这个方法只能清理当前application下的缓存, 所以就只有另找方法,在网上看了说把前台和后台的缓存都放common目录下去, 但这样做需要在开发的时候避免冲突,所以我是直接使用FileCache的gc方法清理
在yii的FileCache中有2个东西:
public $cachePath = '@runtime/cache';
表示缓存目录
在这个文件中没有flush()方法,但是有个flushValues()方法, 但问题是这个方法是受保护方法:
protected function flushValues()
{
$this->gc(true, false);
return true;
}
再看里面的代码,调用了gc()方法,在找到gc()
/**
* Removes expired cache files.
* @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]].
* Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
* @param bool $expiredOnly whether to removed expired cache files only.
* If false, all cache files under [[cachePath]] will be removed.
*/
public function gc($force = false, $expiredOnly = true)
{
if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$this->gcRecursive($this->cachePath, $expiredOnly);
}
}
所以我直接调用这个gc()方法去清理前端的缓存:
$cache = new \yii\caching\FileCache();
$cache->cachePath = '../../frontend/runtime/cache';
$cache->gc(true, false);