yii 高级版后台清理前台的缓存

我使用的是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);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值