Skip to content

Commit

Permalink
perf: cache getAttributeNames
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideIadeluca committed Nov 19, 2024
1 parent e761327 commit 19ffca7
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion framework/core/src/Foundation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,8 @@ abstract class AbstractValidator
{
use ExtensionIdTrait;

public static string $CORE_VALIDATION_CACHE_KEY = 'core.validation.extension_id_class_names';

/**
* @var array
*/
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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 = [];

Expand All @@ -96,6 +109,8 @@ protected function getAttributeNames()
$attributeNames[$attribute] = $this->translator->trans($key);
}

$this->cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames);

return $attributeNames;
}

Expand Down

0 comments on commit 19ffca7

Please sign in to comment.