0

I have a problem: When I'm trying to flush Magento cache programmatically like this:

$types=array('config','layout','block_html','translate','collections','eav','config_api','config_api2');
foreach($types as $type) {
    $c = Mage::app()->getCacheInstance()->cleanType($type);
    Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => $type));
}

[source]

I have already read this article about Magento cache and it's flushing, but I'm still missing something.

The above method should do the same as does the "Flush Magento Cache" button, but it doesn't in my case. When I'm running a script I'm saving a new controller, but it doesn't work after cache flushing programmatically with the way described above (and many others I have already tried).

But as soon as I perform the same from admin panel manually in the middle of the script's job- it starts working correctly.

Any idea? Does it help, if I will put here scripts code?

Thanks in advance!

Vitali
  • 549
  • 6
  • 16
  • Does it mean that you don't see your new controller class after flushing the cache or the url for the controller is not working and showing you 404? – Long M K Nguyễn Jan 13 '18 at 00:24
  • Where should I see my controller? In admin panel you mean? - Nope, it's not there after automatic cache flushing. And after manual appears. – Vitali Jan 13 '18 at 18:12

1 Answers1

0

Your code seems to be clearing only the block caches, for new controllers/classes, you will need to remove the cache folder, you can do it programmatically like this

/**
 * Remove cache folders:
 */
public function cleanFiles() {
    try {
        flush();
        //cache
        $dir = Mage::getBaseDir('cache');
        $items = array_diff(scandir($dir), array('..', '.'));
        foreach ($items as $item) {
            $path = $dir . DIRECTORY_SEPARATOR . $item;
            is_dir($path) ? $this->_rrmdir($path) : unlink($path);
        }
    } catch (Exception $e) {
        die("[ERROR:" . $e->getMessage() . "]" . PHP_EOL);
    }
}

/**
 * Removes a directory and all elements contained
 * @param string $dir directory to remove
 */
private function _rrmdir($dir) {
    if (is_dir($dir)) {
        $objects = array_diff(scandir($dir), array('..', '.'));
        foreach ($objects as $object) {
            $path = $dir . DIRECTORY_SEPARATOR . $object;
            is_dir($path) ? $this->_rrmdir($path) : unlink($path);
        }
        reset($objects);
        rmdir($dir);
    }
}
  • I'll give it a chance today. Thanks for reply. Hope it will help – Vitali Jan 16 '18 at 06:56
  • Hope it will help with your issue, if it works please mark the solution as accepted answer, best of luck – Long M K Nguyễn Jan 16 '18 at 19:22
  • This solution is an equivalent of the another button (Flush Cache storage). But I was thinking rather about the reason, why the left button (Flush Magento cache), that does the same as function in my question, doesn't work, when the button from admin panel works. Is there any kind of session running and it couldn't be removed, or what is different? – Vitali Jan 17 '18 at 16:52
  • Flush Magento cache clears the block caches as i mentioned above, or to be specific, the things that Magento controls. While Flush Cache storage will remove the cache folders. A more details answer is here https://stackoverflow.com/questions/5955365/what-is-the-difference-between-flush-magento-cache-and-flush-cache-storage-i Did it work for you eventually? if it does please accept it as the answer – Long M K Nguyễn Jan 17 '18 at 22:55
  • But I would like to remove cache for my controller without deleting all of the cache – Vitali Jan 18 '18 at 10:30
  • I don't think that would be possible, or you can update your original question to be clearer so that someone else can help with what you need to achieve – Long M K Nguyễn Jan 19 '18 at 17:13