From 2b1ded2f86048c4586dfa985c0634a3e02ec27ad Mon Sep 17 00:00:00 2001 From: "Claude (Deployyy workspace)" Date: Tue, 20 Oct 2026 22:48:39 +0000 Subject: [PATCH] =?UTF-8?q?Keep=20the=20directory=20section=20of=20the=20c?= =?UTF-8?q?ustomer=20data=20per=20store=20view=20in=20the=20cache,=20so=20?= =?UTF-8?q?a=20theme=20that=20inlines=20the=20default=20section=20data=20o?= =?UTF-8?q?n=20every=20page,=20such=20as=20Hyv=C3=A4,=20no=20longer=20sele?= =?UTF-8?q?cts=20the=20regions=20and=20countries=20and=20looks=20up=20ever?= =?UTF-8?q?y=20country=20name=20on=20each=20request.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- dev/README.md | 1 + .../Directory/DirectoryDataFromCache.php | 47 +++++++++++ .../Directory/DirectoryDataFromCacheTest.php | 77 +++++++++++++++++++ src/FastBoot/etc/di.xml | 4 + src/FastBoot/etc/module.xml | 1 + src/FastBootCache/Model/Feature.php | 2 +- 6 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/FastBoot/Plugin/Directory/DirectoryDataFromCache.php create mode 100644 src/FastBoot/Test/Unit/Plugin/Directory/DirectoryDataFromCacheTest.php diff --git a/dev/README.md b/dev/README.md index da26e4c..7c44fe7 100644 --- a/dev/README.md +++ b/dev/README.md @@ -30,6 +30,7 @@ Production installs use the default-enabled optimizations. These switches suppor | `tax_rates` | Tax rates and applied rates per rate request from the cache, for requests that are not customer-specific. | Yes | | `customer_groups` | Customer groups by id from the cache, with their excluded websites. | Yes | | `currency_rates` | Currency rates from the cache; a rate import cleans them. | Yes | +| `directory_data` | The directory section (countries, regions) per store view from the cache; a config save or a translation change cleans it. | Yes | | `config_memo` | The configuration values a request read, by scope, scope code and path; a clean, a mutable set and a store switch empty it. | No | | `stock_id` | The stock id of the current website from the cache; a change of a stock's sales channels cleans it. | Yes | diff --git a/src/FastBoot/Plugin/Directory/DirectoryDataFromCache.php b/src/FastBoot/Plugin/Directory/DirectoryDataFromCache.php new file mode 100644 index 0000000..3c2a308 --- /dev/null +++ b/src/FastBoot/Plugin/Directory/DirectoryDataFromCache.php @@ -0,0 +1,47 @@ +feature->on(self::SWITCH)) { + return $proceed(); + } + $key = self::KEY . (int)$this->storeManager->getStore()->getId(); + $cached = $this->cache->load($key); + $data = is_string($cached) ? json_decode($cached, true) : null; + if (is_array($data)) { + return $data; + } + $data = $proceed(); + $this->cache->save(json_encode($data), $key, [ConfigCache::CACHE_TAG, TranslateCache::CACHE_TAG]); + + return $data; + } +} diff --git a/src/FastBoot/Test/Unit/Plugin/Directory/DirectoryDataFromCacheTest.php b/src/FastBoot/Test/Unit/Plugin/Directory/DirectoryDataFromCacheTest.php new file mode 100644 index 0000000..4ec5705 --- /dev/null +++ b/src/FastBoot/Test/Unit/Plugin/Directory/DirectoryDataFromCacheTest.php @@ -0,0 +1,77 @@ +createMock(CacheInterface::class); + $cache->method('load')->willReturnCallback(fn(string $id) => $this->entries[$id] ?? false); + $cache->method('save')->willReturnCallback(function (string $data, string $id, array $tags) { + $this->entries[$id] = $data; + $this->tags[$id] = $tags; + + return true; + }); + $feature = $this->createStub(Feature::class); + $feature->method('on')->willReturn($on); + $store = $this->createStub(StoreInterface::class); + $store->method('getId')->willReturnCallback(fn() => $this->storeId); + $stores = $this->createStub(StoreManagerInterface::class); + $stores->method('getStore')->willReturn($store); + + return new DirectoryDataFromCache($cache, $feature, $stores); + } + + public function testTheSectionIsComputedOncePerStoreView(): void + { + $plugin = $this->plugin(); + $subject = $this->createStub(DirectoryData::class); + $calls = 0; + $proceed = function () use (&$calls) { + $calls++; + + return ['NL' => ['name' => 'Netherlands' . $calls, 'regions' => [12 => ['code' => 'NH', 'name' => 'Noord-Holland']]]]; + }; + + $first = $plugin->aroundGetSectionData($subject, $proceed); + self::assertSame($first, $plugin->aroundGetSectionData($subject, $proceed)); + self::assertSame(1, $calls); + self::assertSame('Noord-Holland', $first['NL']['regions'][12]['name']); + self::assertSame(['CONFIG', 'TRANSLATE'], $this->tags['FASTBOOT_DIRECTORY_DATA_1']); + + $this->storeId = 2; + self::assertSame('Netherlands2', $plugin->aroundGetSectionData($subject, $proceed)['NL']['name']); + self::assertSame(2, $calls); + } + + public function testTheSwitchOffComputesEveryTime(): void + { + $plugin = $this->plugin(false); + $calls = 0; + $proceed = function () use (&$calls) { + $calls++; + + return []; + }; + $plugin->aroundGetSectionData($this->createStub(DirectoryData::class), $proceed); + $plugin->aroundGetSectionData($this->createStub(DirectoryData::class), $proceed); + + self::assertSame(2, $calls); + self::assertSame([], $this->entries); + } +} diff --git a/src/FastBoot/etc/di.xml b/src/FastBoot/etc/di.xml index 130c3f6..afa31ab 100644 --- a/src/FastBoot/etc/di.xml +++ b/src/FastBoot/etc/di.xml @@ -10,6 +10,7 @@ FASTBOOT_TAX_RATE_ FASTBOOT_GROUP_ FASTBOOT_CURRENCY_RATE_ + FASTBOOT_DIRECTORY_DATA_ GraphCommerce\FastBootCache\Model\Tag::TAX @@ -80,6 +81,9 @@ + + + diff --git a/src/FastBoot/etc/module.xml b/src/FastBoot/etc/module.xml index 6ea29ec..6ac0c4e 100644 --- a/src/FastBoot/etc/module.xml +++ b/src/FastBoot/etc/module.xml @@ -10,6 +10,7 @@ + diff --git a/src/FastBootCache/Model/Feature.php b/src/FastBootCache/Model/Feature.php index abcf41b..6be3ad0 100644 --- a/src/FastBootCache/Model/Feature.php +++ b/src/FastBootCache/Model/Feature.php @@ -15,7 +15,7 @@ class Feature { private ?array $switches = null; - private const CACHED = ['schema_array' => true,'cache_files' => true,'system_config_array' => true,'view_config' => true,'parsed_queries' => true,'validated_queries' => true,'scopes_cache' => true,'website_stores' => true,'deploy_config_unchanged' => true,'placeholder_url' => true,'guest_tax_factor' => true,'tax_rates' => true,'customer_groups' => true,'currency_rates' => true,'stock_id' => true]; + private const CACHED = ['schema_array' => true,'cache_files' => true,'system_config_array' => true,'view_config' => true,'parsed_queries' => true,'validated_queries' => true,'scopes_cache' => true,'website_stores' => true,'deploy_config_unchanged' => true,'placeholder_url' => true,'guest_tax_factor' => true,'tax_rates' => true,'customer_groups' => true,'currency_rates' => true,'directory_data' => true,'stock_id' => true]; public function __construct( private readonly DeploymentConfig $deploymentConfig,