Skip to content

Instantly share code, notes, and snippets.

@bjarneo
Last active November 30, 2019 04:04
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bjarneo/cc32ca547c40bd44148f to your computer and use it in GitHub Desktop.
CLI clear cache. Add this file to shell/. Repo: https://github.com/bjarneo/MagentoCacheTool
<?php
/**
* Clear cache CLI style
*
*
* @category Mage
* @package Mage_Shell
* @copyright Copyright (c) 2015 Bjarne Oeverli (http://bjarneo.codes)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
require_once 'abstract.php';
/**
* Magento Cache Shell Script
*
* @category Mage
* @package Mage_Shell
* @author Bjarne Oeverli <bjarne.oeverli@gmail.com>
*/
class Mage_Shell_Cache extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
if ($this->getArg('clear-storage')) {
$this->_clearStorageCache();
} else if ($this->getArg('clear-image')) {
$this->_clearImageCache();
} else if ($this->getArg('clear-assets')) {
$this->_clearAssetsCache();
} else if ($this->getArg('clear-swatches')) {
$this->_clearSwatchesCache();
} else if ($this->getArg('clear-system')) {
$this->_clearSystemCache();
} else if ($this->getArg('clear-all')) {
$this->_clearAll();
} else {
echo $this->_usageHelp();
}
}
/**
* Get Mage app
*
*/
private function _getApp()
{
return Mage::app();
}
/**
* Get product image model
*
*/
private function _getProductImageModel()
{
return Mage::getModel('catalog/product_image');
}
/**
* Dispatch event
*
*/
private function _dispatchEvent($event)
{
Mage::dispatchEvent($event);
}
/**
* Clear cache
*
*/
private function _clearStorageCache()
{
try {
$this->_getApp()->getCacheInstance()->flush();
$this->_dispatchEvent('shell_cache_flush_all_after');
echo 'Cache cleared ' . PHP_EOL;
} catch (Mage_Core_Exception $e) {
echo $e->getMessage();
}
}
/**
* Clear image cache
*
*/
private function _clearImageCache()
{
try {
$this->_getProductImageModel()->clearCache();
$this->_dispatchEvent('shell_clean_catalog_images_cache_after');
echo 'Image cache cleared ' . PHP_EOL;
} catch (Mage_Core_Exception $e) {
echo $e->getMessage();
}
}
/**
* Clear assets cache
*
*/
private function _clearAssetsCache()
{
try {
Mage::getDesign()->cleanMergedJsCss();
$this->_dispatchEvent('shell_clean_media_cache_after');
echo 'JavaScript and CSS cache cleared ' . PHP_EOL;
} catch (Mage_Core_Exception $e) {
echo $e->getMessage();
}
}
/**
* Clear swatches images cache
*
*/
private function _clearSwatchesCache()
{
try {
Mage::helper('configurableswatches/productimg')->clearSwatchesCache();
$this->_dispatchEvent('shell_configurable_swatches_cache_after');
echo 'Swatches cache cleared ' . PHP_EOL;
} catch (Mage_Core_Exception $e) {
echo $e->getMessage();
}
}
/**
* Clear system cache
*
*/
private function _clearSystemCache()
{
try {
$this->_getApp()->cleanCache();
$this->_dispatchEvent('shell_cache_flush_system_after');
echo 'System cache cleared ' . PHP_EOL;
} catch (Mage_Core_Exception $e) {
echo $e->getMessage();
}
}
private function _clearAll()
{
$this->_clearStorageCache();
$this->_clearImageCache();
$this->_clearAssetsCache();
$this->_clearSwatchesCache();
$this->_clearSystemCache();
$this->_dispatchEvent('shell_clear_cache_all_after');
}
/**
* Retrieve Usage Help Message
*
*/
private function _usageHelp()
{
return <<<USAGE
Usage: php cache.php -- [options]
--clear-storage Clear storage cache
--clear-image Clear image cache
--clear-assets Clear JavaScript and CSS cache
--clear-swatches Clear swatches cache
--clear-system Clear system cache
--clear-all Clear all cache
--help This help
USAGE;
}
}
$shell = new Mage_Shell_Cache();
$shell->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment