5

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

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

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!

Guns
  • 2,678
  • 2
  • 23
  • 51
  • 1
    I suggest debugging - e.g. what's the return value of `Cache::clear`? Does the code [return early](https://github.com/cakephp/cakephp/blob/master/lib/Cake/Cache/Cache.php#L466)? If the return value is false - trace [why that is](https://github.com/cakephp/cakephp/blob/master/lib/Cake/Cache/Engine/FileEngine.php#L227). If you don't find "the answer" but do find more information - add it to the question =). – AD7six Oct 07 '13 at 08:12

1 Answers1

15

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 :)

Thanks

Vinod Patidar
  • 685
  • 4
  • 17
  • 2
    It's sad that all this is required to properly remove the cache files from Cake, even in 2015. But this worked perfectly: the unlink()ing of $files was what did it. – Vael Victus May 05 '15 at 15:00
  • 1
    If one of the folder is empty, the `glob` function may return `false`, and thereby causes the `array_merge` function to throw an error. Because of this I check if there any cache files in the folder: `if(glob(CACHE . 'models' . DS . '*') !== false)` – FrankSunnyman Sep 16 '16 at 10:21