Last active
February 18, 2019 18:31
-
-
Save mirsoftacquia/644885c99c237072fd0167ea8f62ee44 to your computer and use it in GitHub Desktop.
Clear frontend caches only if theme files changed.
This file contains 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
services: | |
my_module.commands: | |
class: \Drupal\my_module\Commands\MyModuleDrushCommands | |
tags: | |
- { name: drush.command } |
This file contains 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
/** | |
* Lighter version of drupal_flush_all_caches() function. | |
* | |
* This function clears frontend caches (css, js and twig) only. | |
* | |
* @see drupal_flush_all_caches() | |
*/ | |
function my_module_flush_frontend_caches() { | |
// Frontend data can remain in these bins, so we need to empty them as well. | |
$bins_to_clear = [ | |
'render', | |
'dynamic_page_cache', | |
'page' | |
]; | |
foreach (Cache::getBins() as $key => $cache_backend) { | |
if (in_array($key, $bins_to_clear)) { | |
$cache_backend->deleteAll(); | |
} | |
} | |
// Flush asset file caches. | |
\Drupal::service('asset.css.collection_optimizer')->deleteAll(); | |
\Drupal::service('asset.js.collection_optimizer')->deleteAll(); | |
_drupal_flush_css_js(); | |
// Wipe the Twig PHP Storage cache. | |
\Drupal::service('twig')->invalidate(); | |
} |
This file contains 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
<?php | |
// Place this file into my_module/src/Commands/MyModuleDrushCommands.php | |
namespace Drupal\my_module\Commands; | |
use Drush\Commands\DrushCommands; | |
/** | |
* Class MyModuleDrushCommands. | |
*/ | |
class MyModuleDrushCommands extends DrushCommands { | |
/** | |
* Clears only frontend caches. | |
* | |
* This is the lighter version of drush cr. It only clears frontend caches, | |
* so it make sense to use it instead of drush cr if only .css, .js, .twig | |
* or .theme files were changed. | |
* | |
* @command cr:frontend | |
* | |
* @aliases crf, cache-rebuild-frontend | |
*/ | |
public function cacheRebuildFrontend() { | |
my_module_flush_frontend_caches(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment