From 19ffca70a0689bbed485a94c2efdfb5a13f4aeae Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 19 Nov 2024 22:38:37 +0100 Subject: [PATCH] perf: cache `getAttributeNames` --- .../core/src/Foundation/AbstractValidator.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/framework/core/src/Foundation/AbstractValidator.php b/framework/core/src/Foundation/AbstractValidator.php index e05a38bffe..3d8b00a7da 100644 --- a/framework/core/src/Foundation/AbstractValidator.php +++ b/framework/core/src/Foundation/AbstractValidator.php @@ -9,6 +9,7 @@ namespace Flarum\Foundation; +use Illuminate\Contracts\Cache\Store as Cache; use Illuminate\Support\Arr; use Illuminate\Validation\Factory; use Illuminate\Validation\ValidationException; @@ -18,6 +19,8 @@ abstract class AbstractValidator { use ExtensionIdTrait; + public static string $CORE_VALIDATION_CACHE_KEY = 'core.validation.extension_id_class_names'; + /** * @var array */ @@ -43,14 +46,20 @@ public function addConfiguration($callable) */ protected $translator; + /** + * @var Cache + */ + protected $cache; + /** * @param Factory $validator * @param TranslatorInterface $translator */ - public function __construct(Factory $validator, TranslatorInterface $translator) + public function __construct(Factory $validator, TranslatorInterface $translator, Cache $cache) { $this->validator = $validator; $this->translator = $translator; + $this->cache = $cache; } /** @@ -88,6 +97,10 @@ protected function getMessages() */ protected function getAttributeNames() { + if ($this->cache->get(self::$CORE_VALIDATION_CACHE_KEY) !== null) { + return $this->cache->get(self::$CORE_VALIDATION_CACHE_KEY); + } + $extId = $this->getClassExtensionId(); $attributeNames = []; @@ -96,6 +109,8 @@ protected function getAttributeNames() $attributeNames[$attribute] = $this->translator->trans($key); } + $this->cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames); + return $attributeNames; }