本文介绍了CakePHP Cache :: clear不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Cache Configuration in my bootstrap.php file as

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

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

and i am trying to clear cache when a setting is edited. Below is my admin_edit function

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);
        }
    }

但是,缓存:: clear(false,'long')不起作用,并且不会清除缓存。不知道出了什么问题。现在卡住了几天!

However, Cache::clear(false,'long') does not work and it does not clear the Cache. Not sure what is going wrong. Stuck for a few days now!

推荐答案

请在任何控制器中使用以下函数,并在希望清除的地方运行该函数

Please use below function in any controller and run that function where you want it will be clear all cache.

    /**
     * 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';
    }

一旦不知道是否为您工作,请让我知道:)

Once let me know if not working for you :)

谢谢

这篇关于CakePHP Cache :: clear不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:35