Skip to content

Instantly share code, notes, and snippets.

@Vinai
Last active January 8, 2019 09:25
Show Gist options
  • Save Vinai/b2bceebbb884dd8057cb39c4bacce1bf to your computer and use it in GitHub Desktop.
Save Vinai/b2bceebbb884dd8057cb39c4bacce1bf to your computer and use it in GitHub Desktop.
Quick to invoke version of bin/magento cache:clean
#!/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);
}
@Vinai
Copy link
Author

Vinai commented Aug 10, 2018

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