Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
47 changes: 47 additions & 0 deletions src/FastBoot/Plugin/Directory/DirectoryDataFromCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

namespace GraphCommerce\FastBoot\Plugin\Directory;

use GraphCommerce\FastBootCache\Model\Feature;
use Magento\Checkout\CustomerData\DirectoryData;
use Magento\Framework\App\Cache\Type\Config as ConfigCache;
use Magento\Framework\App\Cache\Type\Translate as TranslateCache;
use Magento\Framework\App\CacheInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* The directory section from the cache, per store view, instead of the region and country selects
* and a country name lookup per country on every request that renders it; a theme that inlines the
* default section data, such as Hyvä, renders it on every page. Core keeps its region JSON under the
* config tag the same way; the translate tag covers the translated region names.
*/
class DirectoryDataFromCache
{
private const SWITCH = 'directory_data';
private const KEY = 'FASTBOOT_DIRECTORY_DATA_';

public function __construct(
private readonly CacheInterface $cache,
private readonly Feature $feature,
private readonly StoreManagerInterface $storeManager,
) {
}

public function aroundGetSectionData(DirectoryData $subject, callable $proceed): array
{
if (!$this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);

namespace GraphCommerce\FastBoot\Test\Unit\Plugin\Directory;

use GraphCommerce\FastBoot\Plugin\Directory\DirectoryDataFromCache;
use GraphCommerce\FastBootCache\Model\Feature;
use Magento\Checkout\CustomerData\DirectoryData;
use Magento\Framework\App\CacheInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\TestCase;

class DirectoryDataFromCacheTest extends TestCase
{
private array $entries = [];
private array $tags = [];
private int $storeId = 1;

private function plugin(bool $on = true): DirectoryDataFromCache
{
$cache = $this->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);
}
}
4 changes: 4 additions & 0 deletions src/FastBoot/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<item name="tax_rates" xsi:type="string">FASTBOOT_TAX_RATE_</item>
<item name="groups" xsi:type="string">FASTBOOT_GROUP_</item>
<item name="currency_rates" xsi:type="string">FASTBOOT_CURRENCY_RATE_</item>
<item name="directory_data" xsi:type="string">FASTBOOT_DIRECTORY_DATA_</item>
</argument>
<argument name="tags" xsi:type="array">
<item name="tax" xsi:type="const">GraphCommerce\FastBootCache\Model\Tag::TAX</item>
Expand Down Expand Up @@ -80,6 +81,9 @@
<type name="Magento\Directory\Model\ResourceModel\Currency">
<plugin name="graphcommerce_fastboot_currency_rates" type="GraphCommerce\FastBoot\Plugin\Directory\CurrencyRatesFromCache"/>
</type>
<type name="Magento\Checkout\CustomerData\DirectoryData">
<plugin name="graphcommerce_fastboot_directory_data" type="GraphCommerce\FastBoot\Plugin\Directory\DirectoryDataFromCache"/>
</type>
<type name="Magento\Tax\Model\ResourceModel\Calculation\Rate">
<plugin name="graphcommerce_fastboot_forget_rates" type="GraphCommerce\FastBoot\Plugin\Tax\ForgetRates"/>
</type>
Expand Down
1 change: 1 addition & 0 deletions src/FastBoot/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<module name="Magento_Authorization"/>
<module name="Magento_Customer"/>
<module name="Magento_Directory"/>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
2 changes: 1 addition & 1 deletion src/FastBootCache/Model/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down