From 6c78341ae232a327d430047bb47d678eb313d9e8 Mon Sep 17 00:00:00 2001 From: colemanw Date: Sun, 3 Dec 2023 16:20:07 +0000 Subject: [PATCH] Cache - Use service name --- CRM/Extension/System.php | 1 + CRM/Utils/Cache.php | 2 +- CRM/Utils/Cache/CacheWrapper.php | 12 ++++++------ Civi/Core/Container.php | 3 +++ .../phpunit/Civi/Core/Service/AutoDefinitionTest.php | 4 ++-- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CRM/Extension/System.php b/CRM/Extension/System.php index 39854cec24..682097afb3 100644 --- a/CRM/Extension/System.php +++ b/CRM/Extension/System.php @@ -273,6 +273,7 @@ class CRM_Extension_System { // Extension system starts before container. Manage our own cache. $this->cache = CRM_Utils_Cache::create([ 'name' => $cacheGroup, + 'service' => 'extension_system', 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], 'prefetch' => TRUE, 'withArray' => 'fast', diff --git a/CRM/Utils/Cache.php b/CRM/Utils/Cache.php index bb4bf4560b..902631347d 100644 --- a/CRM/Utils/Cache.php +++ b/CRM/Utils/Cache.php @@ -206,7 +206,7 @@ class CRM_Utils_Cache { } } if (isset($cache)) { - return new CRM_Utils_Cache_CacheWrapper($cache, $params['name'] ?? NULL); + return new CRM_Utils_Cache_CacheWrapper($cache, $params['service'] ?? $params['name'] ?? NULL); } throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1)); } diff --git a/CRM/Utils/Cache/CacheWrapper.php b/CRM/Utils/Cache/CacheWrapper.php index 3d8ce16d3d..d6990331c9 100644 --- a/CRM/Utils/Cache/CacheWrapper.php +++ b/CRM/Utils/Cache/CacheWrapper.php @@ -22,7 +22,7 @@ class CRM_Utils_Cache_CacheWrapper implements CRM_Utils_Cache_Interface { /** * @var string */ - private $cacheName; + private $serviceName; /** * @var CRM_Utils_Cache_Interface @@ -31,11 +31,11 @@ class CRM_Utils_Cache_CacheWrapper implements CRM_Utils_Cache_Interface { /** * @param \CRM_Utils_Cache_Interface $delegate - * @param string $cacheName + * @param string $serviceName */ - public function __construct(\CRM_Utils_Cache_Interface $delegate, $cacheName) { + public function __construct(\CRM_Utils_Cache_Interface $delegate, $serviceName) { $this->delegate = $delegate; - $this->cacheName = $cacheName; + $this->serviceName = $serviceName; } public function getMultiple($keys, $default = NULL) { @@ -93,12 +93,12 @@ class CRM_Utils_Cache_CacheWrapper implements CRM_Utils_Cache_Interface { private function dispatchClearEvent($keys = NULL) { // FIXME: When would name ever be empty? - if ($this->cacheName) { + if ($this->serviceName) { $hookParams = [ 'items' => $keys, ]; $event = \Civi\Core\Event\GenericHookEvent::create($hookParams); - Civi::dispatcher()->dispatch("civi.cache.$this->cacheName.clear", $event); + Civi::dispatcher()->dispatch("civi.cache.$this->serviceName.clear", $event); } } diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 8ea153495c..ce8572fc6c 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -188,6 +188,8 @@ class Container { foreach ($basicCaches as $cacheSvc => $cacheGrp) { $definitionParams = [ 'name' => $cacheGrp . (in_array($cacheGrp, $verSuffixCaches) ? $verSuffix : ''), + // FIXME: Uncommenting below causes test failure + // 'service' => $cacheSvc, 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], ]; // For Caches that we don't really care about the ttl for and/or maybe accessed @@ -435,6 +437,7 @@ class Container { $moduleEnvId = md5(\CRM_Core_Config_Runtime::getId()); $angCache = \CRM_Utils_Cache::create([ 'name' => substr('angular_' . $moduleEnvId, 0, 32), + 'service' => 'angular_manager', 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], 'withArray' => 'fast', 'prefetch' => TRUE, diff --git a/tests/phpunit/Civi/Core/Service/AutoDefinitionTest.php b/tests/phpunit/Civi/Core/Service/AutoDefinitionTest.php index ba61ab6a13..37e40ae281 100644 --- a/tests/phpunit/Civi/Core/Service/AutoDefinitionTest.php +++ b/tests/phpunit/Civi/Core/Service/AutoDefinitionTest.php @@ -104,7 +104,7 @@ class AutoDefinitionTest extends \CiviUnitTestCase { $this->assertInstanceOf(\CRM_Utils_Cache_CacheWrapper::class, $instance->cache); $cacheClass = Invasive::get([$instance->cache, 'delegate']); $this->assertInstanceOf(\CRM_Utils_Cache_SqlGroup::class, $cacheClass); - $this->assertEquals('extension_browser', Invasive::get([$instance->cache, 'cacheName'])); + $this->assertEquals('extension_browser', Invasive::get([$cacheClass, 'group'])); } /** @@ -183,7 +183,7 @@ class AutoDefinitionTest extends \CiviUnitTestCase { $cacheWrapper = Invasive::get([$instance, 'cache']); $cacheClass = Invasive::get([$cacheWrapper, 'delegate']); $this->assertInstanceOf(\CRM_Utils_Cache_SqlGroup::class, $cacheClass); - $this->assertEquals('extension_browser', Invasive::get([$cacheWrapper, 'cacheName'])); + $this->assertEquals('extension_browser', Invasive::get([$cacheWrapper, 'serviceName'])); $this->assertEquals('extension_browser', Invasive::get([$cacheClass, 'group'])); } -- 2.25.1