One of the most Magento Enterprise Edition major features is Full Page Cache (FPC). It allows to cache HTML contents of an entire page and display it without making tons of unnecessary database requests and additional operations. It makes Magento run greatly faster and accept more visits at once on the same server configuration. However, features are the most favorite places of insidious bugs..
From time to time, we face with a need to clean the cache after some change has been made, or a new extension has been installed. So, we go to the admin panel to find the magic button ‘Flush Cache Storage’ and push it. Normally, everything is going well: there are no explosions or smell of burning. But in the latest versions of Magento Enterprise Edition users might experience a trouble when after cache cleaning the latest changes aren’t being appeared on the site, products still being shown on the site after they have been removed etc. There are many reasons which may cause the issue, so let us don’t dive into the details to prevent making a book instead of the short article.
To fix the problem with FPC cleaning usually enough to clean the full page cache directory manually. Even so, it’s not the most convenient way. Let’s automate this action somehow. Since Magento has such a great thing as event observers we can use them for this purpose. The event which fires each time when the cache storage is being cleaned is called adminhtml_cache_flush_all. Just create observer’s method that cleans the cache and full_page_cache directories contents, and make your observer listen to the event mentioned above. That’s it, each time when you press the ‘Clean Cache Storage’ button the cache directories will be cleaned physically.
The function, which will help you to process the directories cleaning, may look similar to this one:
function Cleandir($path) { if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..' && is_file($path.'/'.$file)) { if (unlink($path.'/'.$file)) { } else { Mage::log($path . '/' . $file . ' file was not removed', null, 'system.log'); } } else if ($file != '.' && $file != '..' && is_dir($path.'/'.$file)) { Cleandir($path.'/'.$file); if (rmdir($path.'/'.$file)) { } else { Mage::log($path . '/' . $file . ' directory was not removed', null, 'system.log'); } } } closedir($handle); } }
To help you with the solution’s implementation we’ve created a small extension which are working the same way as the described above. You can find it here: https://github.com/Atwix/cacheclean
Just put the extension into your Magento root.
We are always appreciate your attention, so feel free to ask questions and advice your improvements.