CakePHP Cache :: clear不起作用

时间:2013-10-07 07:23:33

标签: php cakephp caching cakephp-2.0

我的bootstrap.php文件中有一个缓存配置

Cache::config('long', array(
        'engine'     => 'File',
        'duration'   => '+1 week',
        'probability'=> 100,
        'mask'       => 0666,
        'path'       => CACHE . 'long' . DS,
    ));

我正在尝试在编辑设置时清除缓存。以下是我的admin_edit函数

public function admin_edit($id = null) {
        if (!$this->Setting->exists($id)) {
            throw new NotFoundException(__('Invalid setting'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Setting->save($this->request->data)) {
                $this->Session->setFlash(__('The setting has been saved'));
                $this->redirect(array('action'=> 'index'));
                Cache::clear(false,'long');
                Cache::gc();
            }else {
                $this->Session->setFlash(__('The setting could not be saved. Please, try again.'));
            }
        }else {
            $options = array('conditions' => array('Setting.' . $this->Setting->primaryKey=> $id));
            $this->request->data = $this->Setting->find('first', $options);
        }
    }

但是,Cache::clear(false,'long')不起作用,并且不会清除缓存。不确定出了什么问题。坚持了几天吧!

1 个答案:

答案 0 :(得分:15)

请在任何控制器中使用以下功能并在您想要的位置运行该功能将清除所有缓存。

    /**
     * function to clear all cache data
     * by default accessible only for admin
     *
     * @access Public
     * @return void
     */
    public function clear_cache() {
        Cache::clear();
        clearCache();

        $files = array();
        $files = array_merge($files, glob(CACHE . '*')); // remove cached css
        $files = array_merge($files, glob(CACHE . 'css' . DS . '*')); // remove cached css
        $files = array_merge($files, glob(CACHE . 'js' . DS . '*'));  // remove cached js           
        $files = array_merge($files, glob(CACHE . 'models' . DS . '*'));  // remove cached models           
        $files = array_merge($files, glob(CACHE . 'persistent' . DS . '*'));  // remove cached persistent           

        foreach ($files as $f) {
            if (is_file($f)) {
                unlink($f);
            }
        }

        if(function_exists('apc_clear_cache')):      
        apc_clear_cache();
        apc_clear_cache('user');
        endif;

        $this->set(compact('files'));
        $this->layout = 'ajax';
    }

让我知道如果不为你工作:)

由于