From dacede24b8d67321de7fc5888b54fa7212e02b9a Mon Sep 17 00:00:00 2001 From: Rias Date: Wed, 12 Jun 2024 20:45:58 +0200 Subject: [PATCH] [5.x] Add cache for nested imported fieldsets (#10280) --- src/Fields/Fields.php | 6 ++- src/Fieldtypes/Replicator.php | 70 +++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/Fields/Fields.php b/src/Fields/Fields.php index d3719552a8..7406f9f0d8 100644 --- a/src/Fields/Fields.php +++ b/src/Fields/Fields.php @@ -149,7 +149,11 @@ public function get($field) public function toPublishArray() { - return $this->fields->values()->map->toPublishArray()->all(); + $blink = md5(json_encode($this->fields)); + + return Blink::once($blink, function () { + return $this->fields->values()->map->toPublishArray()->all(); + }); } public function addValues(array $values) diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index b0f7f555eb..23c0a15bad 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -3,6 +3,7 @@ namespace Statamic\Fieldtypes; use Facades\Statamic\Fieldtypes\RowId; +use Statamic\Facades\Blink; use Statamic\Facades\GraphQL; use Statamic\Fields\Fields; use Statamic\Fields\Fieldtype; @@ -118,12 +119,17 @@ protected function preProcessRow($row, $index) public function fields($set, $index = -1) { - return new Fields( - Arr::get($this->flattenedSetsConfig(), "$set.fields"), - $this->field()->parent(), - $this->field(), - $index - ); + $config = Arr::get($this->flattenedSetsConfig(), "$set.fields"); + $hash = md5($index.json_encode($config)); + + return Blink::once($hash, function () use ($config, $index) { + return new Fields( + $config, + $this->field()->parent(), + $this->field(), + $index + ); + }); } public function extraRules(): array @@ -207,15 +213,21 @@ public function preload() return [$set['_id'] => $this->fields($set['type'], $index)->addValues($set)->meta()->put('_', '_')]; })->toArray(); - $defaults = collect($this->flattenedSetsConfig())->map(function ($set, $handle) { - return $this->fields($handle)->all()->map(function ($field) { - return $field->fieldtype()->preProcess($field->defaultValue()); + $blink = md5(json_encode($this->flattenedSetsConfig())); + + $defaults = Blink::once($blink.'-defaults', function () { + return collect($this->flattenedSetsConfig())->map(function ($set, $handle) { + return $this->fields($handle)->all()->map(function ($field) { + return $field->fieldtype()->preProcess($field->defaultValue()); + })->all(); })->all(); - })->all(); + }); - $new = collect($this->flattenedSetsConfig())->map(function ($set, $handle) use ($defaults) { - return $this->fields($handle)->addValues($defaults[$handle])->meta()->put('_', '_'); - })->toArray(); + $new = Blink::once($blink.'-new', function () use ($defaults) { + return collect($this->flattenedSetsConfig())->map(function ($set, $handle) use ($defaults) { + return $this->fields($handle)->addValues($defaults[$handle])->meta()->put('_', '_'); + })->toArray(); + }); $previews = collect($existing)->map(function ($fields) { return collect($fields)->map(function () { @@ -234,21 +246,25 @@ public function preload() public function flattenedSetsConfig() { - $sets = collect($this->config('sets')); - - // If the first set doesn't have a nested "set" key, it would be the legacy format. - // We'll put it in a "main" group so it's compatible with the new format. - // This also happens in the "sets" fieldtype. - if (! Arr::has($sets->first(), 'sets')) { - $sets = collect([ - 'main' => [ - 'sets' => $sets->all(), - ], - ]); - } + $blink = md5($this->field?->handle().json_encode($this->field?->config())); + + return Blink::once($blink, function () { + $sets = collect($this->config('sets')); + + // If the first set doesn't have a nested "set" key, it would be the legacy format. + // We'll put it in a "main" group so it's compatible with the new format. + // This also happens in the "sets" fieldtype. + if (! Arr::has($sets->first(), 'sets')) { + $sets = collect([ + 'main' => [ + 'sets' => $sets->all(), + ], + ]); + } - return $sets->flatMap(function ($section) { - return $section['sets']; + return $sets->flatMap(function ($section) { + return $section['sets']; + }); }); }