Skip to content
Snippets Groups Projects

Issue #3400450 by Kingdutch: Clear Redis cache bins on Drupal installation

1 file
+ 70
0
Compare changes
  • Side-by-side
  • Inline
+ 70
0
@@ -5,6 +5,9 @@
@@ -5,6 +5,9 @@
* Redis install related functions.
* Redis install related functions.
*/
*/
 
use Drupal\Core\Cache\Cache;
 
use Drupal\Core\Serialization\Yaml;
 
use Drupal\redis\Cache\CacheBase;
use \Drupal\redis\ClientFactory;
use \Drupal\redis\ClientFactory;
/**
/**
@@ -38,3 +41,70 @@ function redis_requirements($phase) {
@@ -38,3 +41,70 @@ function redis_requirements($phase) {
return $requirements;
return $requirements;
}
}
 
 
/**
 
* Implements hook_install().
 
*/
 
function redis_install(bool $is_syncing) : void {
 
$cache_backend_factory = \Drupal::service('cache.backend.redis');
 
// Delete all cache bins backed by Redis when installing since processes like
 
// Drush might otherwise get confused. This needs to happen regardless of
 
// whether we install from scratch or from config.
 
foreach (Cache::getBins() as $id => $bin) {
 
// During install the bins will not yet be configured to use Redis, so for
 
// cleanup we treat every bin as if it's in Redis.
 
if (!$bin instanceof CacheBase) {
 
$bin = $cache_backend_factory->get($id);
 
}
 
 
$bin->deleteAll();
 
}
 
}
 
 
/**
 
* Implements hook_modules_installed().
 
*/
 
function redis_modules_installed(array $modules, bool $is_syncing) : void {
 
foreach ($modules as $module) {
 
_redis_clear_redis_cache_bins($module);
 
}
 
}
 
 
/**
 
* Clear all cache bins for a specific module.
 
*
 
* The code in this function is copied from `ModuleInstaller::removeCacheBins`
 
* with the exception that it calls `deleteAll` instead of `removeBin` and
 
* filters for Redis cache bins rather than normal cache bins.
 
*
 
* @param string $module
 
* The module to clear cache bins for.
 
*/
 
function _redis_clear_redis_cache_bins(string $module) : void {
 
$service_yaml_file = \Drupal::service('extension.list.module')->getPath($module) . "/$module.services.yml";
 
if (!file_exists($service_yaml_file)) {
 
return;
 
}
 
 
$definitions = Yaml::decode(file_get_contents($service_yaml_file));
 
 
$cache_bin_services = array_filter(
 
$definitions['services'] ?? [],
 
function ($definition) {
 
$tags = $definition['tags'] ?? [];
 
foreach ($tags as $tag) {
 
if (isset($tag['name']) && ($tag['name'] === 'cache.bin')) {
 
return TRUE;
 
}
 
}
 
return FALSE;
 
}
 
);
 
 
foreach (array_keys($cache_bin_services) as $service_id) {
 
$backend = \Drupal::getContainer()->get($service_id);
 
if ($backend instanceof CacheBase) {
 
$backend->deleteAll();
 
}
 
}
 
}
Loading