Last active
January 8, 2019 09:25
-
-
Save Vinai/b2bceebbb884dd8057cb39c4bacce1bf to your computer and use it in GitHub Desktop.
Quick to invoke version of bin/magento cache:clean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php declare(strict_types=1); | |
use function array_reduce as reduce; | |
use function array_slice as slice; | |
use function array_filter as filter; | |
use function array_map as map; | |
$basedir = reduce(['.', '..', '../..', '../../..'], function ($acc, $basedir) { | |
return file_exists($basedir . '/vendor/autoload.php') ? $basedir : $acc; | |
}, false); | |
if (! $basedir) { | |
fwrite(STDERR, "Unable to locate the vendor/autoload.php file.\n"); | |
exit(2); | |
} | |
require_once $basedir . '/vendor/autoload.php'; | |
function isFullPage(string $tag): bool | |
{ | |
return 'full_page' === $tag; | |
} | |
function isNotFullPage(string $tag): bool | |
{ | |
return ! isFullPage($tag); | |
} | |
function cacheIdPrefix(): string | |
{ | |
return substr(md5(realpath($GLOBALS['basedir']) . '/app/etc/'), 0, 3) . '_'; | |
} | |
function typeToTag(string $tag): string | |
{ | |
$typeToTagMap = [ | |
'collections' => 'COLLECTION_DATA', | |
'config_webservice' => 'WEBSERVICE', | |
'layout' => 'LAYOUT_GENERAL_CACHE_TAG', | |
'full_page' => 'FPC', | |
'config_integration_consolidated' => 'INTEGRATION_CONSOLIDATED', | |
'config_integration_api' => 'INTEGRATION_API_CONFIG', | |
'config_integration' => 'INTEGRATION', | |
]; | |
return cacheIdPrefix() . strtoupper($typeToTagMap[$tag] ?? $tag); | |
} | |
$tags = slice($argv, 1); | |
$cacheTags = filter($tags, 'isNotFullPage'); | |
if (empty($tags) || $cacheTags) { | |
$cache = new Cm_Cache_Backend_File([ | |
'cache_dir' => $basedir . '/var/cache/', | |
'hashed_directory_level' => 1, | |
'file_name_prefix' => 'mage', | |
]); | |
$cache->clean( | |
empty($cacheTags) ? Zend_Cache::CLEANING_MODE_ALL : Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, | |
map('typeToTag', $cacheTags) | |
); | |
} | |
if (empty($tags) || filter($tags, 'isFullPage')) { | |
$pageCache = new Cm_Cache_Backend_File([ | |
'cache_dir' => $basedir . '/var/page_cache/', | |
'hashed_directory_level' => 1, | |
'file_name_prefix' => 'mage', | |
]); | |
$pageCache->clean(Zend_Cache::CLEANING_MODE_ALL); | |
} |
For the record, on my 2012 MBP running bin/magento cache:clean
takes about 1 second, the above script takes about 0.06 seconds.
In case someone stumbles over this gist, check out https://github.com/mage2tv/magento-cache-clean which is an evolved version with a file watcher.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick Magento File Cache Backend Cleaner
This script is intended as a tool to save time during development.
It allows cleaning the Magento file cache backend more quickly than the native
bin/magento cache:clean
CLI command.The problem with
bin/magento
is that it's very slow bootstrap. Cleaning the cache tags with the file system backend takes only a fraction of the bootstrapping time.This script was inspired by Timon de Groot's blog post https://blog.timpack.org/speed-up-magento-development
He has the brilliant idea to use a file watcher in PHPStorm to call redis-cli to clear the complete cache whenever a XML file is modified.
The motivation for this script is to be able to use a more granular cache cleaning approach based on cache tags via a file watcher,
as rebuilding the cache takes so much longer than cleaning it, regardless of the cleaning strategy (even with
bin/magento
)So this current script is only the first step.
The next step is to build a file watcher that will call this script with the appropriate tags.
There is only so much that can be done, but at least two of the most common cases can be covered:
Maybe something can also be done in regards to changes in
routes.xml
,webapi.xml
and maybe even new controller action classes (which require the config cache to be cleaned).Of course it also can be used manually.
If this goes somewhere I'll make a composer package out of it that can be added as a
require-dev
dependency to projects.