codeigniter缓存驱动程序函数clean()不起作用

时间:2012-11-17 11:36:36

标签: php codeigniter

我在codeigniter中使用基于文件的缓存它成功设置缓存,但无法清除它。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tm extends CI_Controller {

public function index(){

    $this->load->driver('cache');

    if ($foo = $this->cache->file->get('foo2'))
        echo $foo.'<br />';
    else
        {
        echo 'Saving to the cache!<br />';
        $foo = 'foobarbaz! :D';
        $this->cache->file->save('foo2', $foo, 60);
        }
    if($this->cache->clean())
        echo "clearing cache ";
    else
        echo "Error clearing cache ";
    }

} 

第一个输出:

  

保存到缓存!

     

清除缓存

当我刷新它

  

foobarbaz! :d

     

清除缓存

看起来$this->cache->clean()函数在失败时总是返回 true

1 个答案:

答案 0 :(得分:0)

在编辑我自己的上一条评论时,我意识到我犯了与你完全相同的错误 - 在->file电话上错过了clear()

Cache类的默认适配器是'dummy',并且您没有将新的适配器传递给初始化程序,因此调用$this->cache->clear()会尝试调用$this->cache->dummy->clear()而不是$this->cache->file->clear()

您必须明确调用$this->cache->file->clear()或在加载库时执行此操作:

$this->load->library('cache', array('adapter' => 'file'));

编辑:

如果

CI_Cache_file无法删除具有缓存路径的任何或所有文件,它仍将返回TRUE。它只调用delete_files()文件助手,它不检查unlink()的返回值。你无法解决这个问题,因为没有像数据库那样的文件删除事务。