From b1b4a32ca0d1cbee4794964c1ac9812c19564e5a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 28 Oct 2021 15:06:46 -0400 Subject: [PATCH] APIv4 - Cache SchemaMap in Civi::cache('metadata') When adding/removing custom groups, the schema map changes. Civi already flushes the metadata cache when this happens, but the APIv4 SchemaMap was being cached in memory (in a container service). This changes it to be stored in the metadata cache like everything else. --- Civi/Api4/Action/Entity/GetLinks.php | 3 +-- Civi/Api4/Query/Api4SelectQuery.php | 4 ++-- Civi/Api4/Utils/CoreUtil.php | 13 +++++++++++++ Civi/Api4/services.xml | 10 +--------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Civi/Api4/Action/Entity/GetLinks.php b/Civi/Api4/Action/Entity/GetLinks.php index e0458b56b5..3408fea35b 100644 --- a/Civi/Api4/Action/Entity/GetLinks.php +++ b/Civi/Api4/Action/Entity/GetLinks.php @@ -25,8 +25,7 @@ class GetLinks extends \Civi\Api4\Generic\BasicGetAction { public function getRecords() { \CRM_Core_Error::deprecatedWarning('APIv4 Entity::getLinks is deprecated.'); $result = []; - /** @var \Civi\Api4\Service\Schema\SchemaMap $schema */ - $schema = \Civi::container()->get('schema_map'); + $schema = CoreUtil::getSchemaMap(); foreach ($schema->getTables() as $table) { $entity = CoreUtil::getApiNameFromTableName($table->getName()); // Since this is an api function, exclude tables that don't have an api diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index ff1590c32e..ea97c7d559 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -13,6 +13,7 @@ namespace Civi\Api4\Query; use Civi\API\Exception\UnauthorizedException; use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable; +use Civi\Api4\Service\Schema\Joiner; use Civi\Api4\Utils\FormattingUtil; use Civi\Api4\Utils\CoreUtil; use Civi\Api4\Utils\SelectUtil; @@ -1004,8 +1005,7 @@ class Api4SelectQuery { if (isset($this->apiFieldSpec[$key])) { return; } - /** @var \Civi\Api4\Service\Schema\Joiner $joiner */ - $joiner = \Civi::container()->get('joiner'); + $joiner = new Joiner(CoreUtil::getSchemaMap()); $pathArray = explode('.', $key); // The last item in the path is the field name. We don't care about that; we'll add all fields from the joined entity. diff --git a/Civi/Api4/Utils/CoreUtil.php b/Civi/Api4/Utils/CoreUtil.php index 62ee8124d9..f77607835c 100644 --- a/Civi/Api4/Utils/CoreUtil.php +++ b/Civi/Api4/Utils/CoreUtil.php @@ -230,4 +230,17 @@ class CoreUtil { return static::checkAccessRecord($apiRequest, $record, $userID); } + /** + * @return \Civi\Api4\Service\Schema\SchemaMap + */ + public static function getSchemaMap() { + $cache = \Civi::cache('metadata'); + $schemaMap = $cache->get('api4.schema.map'); + if (!$schemaMap) { + $schemaMap = \Civi::service('schema_map_builder')->build(); + $cache->set('api4.schema.map', $schemaMap); + } + return $schemaMap; + } + } diff --git a/Civi/Api4/services.xml b/Civi/Api4/services.xml index 630a646326..fe1ca2efc3 100644 --- a/Civi/Api4/services.xml +++ b/Civi/Api4/services.xml @@ -6,18 +6,10 @@ - + - - - - - - - - -- 2.25.1