PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Invalidate CakePHP Model Cache without turning on Debug

 January 23, 2022     caching, cakephp, cakephp-2.0     No comments   

Issue

Is there a convenient way to invalidate the model cache without changing the debug level in a production environment. Currently we have to set:

Configure::write('debug', *1 or 2*);

in order to clear the model cache after an update to production. This is less than optimal since there is potential for end users to see debug messages or warnings that they were not intended to see.

We are not using file cache so it isn't as easy as just deleting the cache files from the /tmp/cache/models/ directory.

Thank you in advance.


Solution

This has been open for a while with no answers, so I wrote my own solution. Here is a function you can use to clear your caches as needed.

I created a Controller for Admin Tools with $uses = false so it doesn't look for an associated Model/Table. Then I created a function to show all of the engines that are currently configured. When a configured cache engine is selected by the user, it calls the __clearCache() function:

public function admin_cache_manager($clear=false, $engineName=''){
        $this->layout = 'manager';
        $engines = Cache::configured();
        if($clear){
            if($engineName == 'all'){
                $status = $this->__clearCache($engines);
            }
            else{
                $clearEngine[0] = $engineName;
                $status = $this->__clearCache($clearEngine);
            }
            $this->set(compact('status'));
        }
        foreach($engines as $key => $engine){
            $configs[$key] = Cache::config($engine);
        }
        $this->set(compact('engines'));
        $this->set(compact('configs'));
    }

The __clearCache() function actually clears the configured cache and returns the status:

private function __clearCache($engines=array(), $check=false){
        foreach($engines as $engine){
            if(Cache::config($engine)){
                if(Cache::clear($check, $engine)){
                    $status[$engine] = 'Cache Cleared';
                }
                else{
                    $status[$engine] = 'Clear Cache Failed';
                }
            }
            else{
                $status[$engine] = 'Engine Not Configured.';
            }
        }
        return $status;
    }


Answered By - JadedCore
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing