Skip to content

Commit 30c98b4

Browse files
committed
B2B-2258: Add caching capability to the storeConfig GraphQl query
1 parent b2f5f0f commit 30c98b4

File tree

4 files changed

+84
-20
lines changed

4 files changed

+84
-20
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\StoreGraphQl\Plugin;
8+
9+
use Magento\StoreGraphQl\Model\Resolver\Store\ConfigIdentity;
10+
11+
/**
12+
* Store plugin
13+
*/
14+
class Store
15+
{
16+
/**
17+
* Add graphql store config tag to the store cache identities.
18+
*
19+
* @param \Magento\Store\Model\Store $subject
20+
* @param array $result
21+
* @return array
22+
*/
23+
public function afterGetIdentities(\Magento\Store\Model\Store $subject, array $result): array
24+
{
25+
return array_merge($result, [sprintf('%s_%s', ConfigIdentity::CACHE_TAG, $subject->getId())]);
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\StoreGraphQl\Plugin;
8+
9+
use Magento\StoreGraphQl\Model\Resolver\Store\ConfigIdentity;
10+
11+
/**
12+
* Website plugin
13+
*/
14+
class Website
15+
{
16+
/**
17+
* Add graphql store config tag to the website cache identities.
18+
*
19+
* @param \Magento\Store\Model\Website $subject
20+
* @param array $result
21+
* @return array
22+
*/
23+
public function afterGetIdentities(\Magento\Store\Model\Website $subject, array $result): array
24+
{
25+
$storeIds = $subject->getStoreIds();
26+
foreach ($storeIds as $storeId) {
27+
$result[] = sprintf('%s_%s', ConfigIdentity::CACHE_TAG, $storeId);
28+
}
29+
return $result;
30+
}
31+
}

app/code/Magento/StoreGraphQl/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
</argument>
1414
</arguments>
1515
</type>
16+
<type name="Magento\Store\Model\Store">
17+
<plugin name="getIdentities" type="Magento\StoreGraphQl\Plugin\Store" />
18+
</type>
19+
<type name="Magento\Store\Model\Website">
20+
<plugin name="getIdentities" type="Magento\StoreGraphQl\Plugin\Website" />
21+
</type>
1622
</config>

dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigCacheTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testGetStoreConfig(): void
9494
$responseDefaultStore = $this->graphQlQueryWithResponseHeaders($query);
9595
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $responseDefaultStore['headers']);
9696
$defaultStoreCacheId = $responseDefaultStore['headers'][CacheIdCalculator::CACHE_ID_HEADER];
97-
// Verify we obtain a cache MISS the 1st time
97+
// Verify we obtain a cache MISS at the 1st time
9898
$defaultStoreResponse = $this->assertCacheMissAndReturnResponse(
9999
$query,
100100
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]
@@ -104,7 +104,7 @@ public function testGetStoreConfig(): void
104104
$this->assertEquals($defaultStoreId, $defaultStoreResponseResult['id']);
105105
$this->assertEquals($defaultStoreCode, $defaultStoreResponseResult['code']);
106106
$this->assertEquals($defaultLocale, $defaultStoreResponseResult['locale']);
107-
// Verify we obtain a cache HIT the 2nd time
107+
// Verify we obtain a cache HIT at the 2nd time
108108
$defaultStoreResponseHit = $this->assertCacheHitAndReturnResponse(
109109
$query,
110110
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]
@@ -121,7 +121,7 @@ public function testGetStoreConfig(): void
121121
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $responseTestStore['headers']);
122122
$testStoreCacheId = $responseTestStore['headers'][CacheIdCalculator::CACHE_ID_HEADER];
123123
$this->assertNotEquals($testStoreCacheId, $defaultStoreCacheId);
124-
// Verify we obtain a cache MISS the 1st time
124+
// Verify we obtain a cache MISS at the 1st time
125125
$testStoreResponse = $this->assertCacheMissAndReturnResponse(
126126
$query,
127127
[
@@ -133,7 +133,7 @@ public function testGetStoreConfig(): void
133133
$testStoreResponseResult = $testStoreResponse['body']['storeConfig'];
134134
$this->assertEquals($testStoreCode, $testStoreResponseResult['code']);
135135
$this->assertEquals($defaultLocale, $testStoreResponseResult['locale']);
136-
// Verify we obtain a cache HIT the 2nd time
136+
// Verify we obtain a cache HIT at the 2nd time
137137
$testStoreResponseHit = $this->assertCacheHitAndReturnResponse(
138138
$query,
139139
[
@@ -165,7 +165,7 @@ public function testCachePurgedWithStoreScopeConfigChange(): void
165165
$responseDefaultStore = $this->graphQlQueryWithResponseHeaders($query);
166166
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $responseDefaultStore['headers']);
167167
$defaultStoreCacheId = $responseDefaultStore['headers'][CacheIdCalculator::CACHE_ID_HEADER];
168-
// Verify we obtain a cache MISS the 1st time
168+
// Verify we obtain a cache MISS at the 1st time
169169
$defaultStoreResponse = $this->assertCacheMissAndReturnResponse(
170170
$query,
171171
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]
@@ -182,7 +182,7 @@ public function testCachePurgedWithStoreScopeConfigChange(): void
182182
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $responseTestStore['headers']);
183183
$testStoreCacheId = $responseTestStore['headers'][CacheIdCalculator::CACHE_ID_HEADER];
184184
$this->assertNotEquals($testStoreCacheId, $defaultStoreCacheId);
185-
// Verify we obtain a cache MISS the 1st time
185+
// Verify we obtain a cache MISS at the 1st time
186186
$testStoreResponse = $this->assertCacheMissAndReturnResponse(
187187
$query,
188188
[
@@ -201,19 +201,19 @@ public function testCachePurgedWithStoreScopeConfigChange(): void
201201
$this->setConfig($localeConfigPath, $newLocale, ScopeInterface::SCOPE_STORE, $testStoreCode);
202202

203203
// Query default store config after test store config change
204-
// Verify we obtain a cache HIT the 2nd time, the cache is not purged
205-
$defaultStoreResponseHit2 = $this->assertCacheHitAndReturnResponse(
204+
// Verify we obtain a cache HIT at the 2nd time, the cache is not purged
205+
$defaultStoreResponseHit= $this->assertCacheHitAndReturnResponse(
206206
$query,
207207
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]
208208
);
209-
$this->assertArrayHasKey('storeConfig', $defaultStoreResponseHit2['body']);
210-
$defaultStoreResponseHit2Result = $defaultStoreResponseHit2['body']['storeConfig'];
211-
$this->assertEquals($defaultStoreId, $defaultStoreResponseHit2Result['id']);
212-
$this->assertEquals($defaultStoreCode, $defaultStoreResponseHit2Result['code']);
213-
$this->assertEquals($defaultLocale, $defaultStoreResponseHit2Result['locale']);
209+
$this->assertArrayHasKey('storeConfig', $defaultStoreResponseHit['body']);
210+
$defaultStoreResponseHitResult = $defaultStoreResponseHit['body']['storeConfig'];
211+
$this->assertEquals($defaultStoreId, $defaultStoreResponseHitResult['id']);
212+
$this->assertEquals($defaultStoreCode, $defaultStoreResponseHitResult['code']);
213+
$this->assertEquals($defaultLocale, $defaultStoreResponseHitResult['locale']);
214214

215215
// Query test store config after test store config change
216-
// Verify we obtain a cache MISS the 2nd time, the cache is purged
216+
// Verify we obtain a cache MISS at the 2nd time, the cache is purged
217217
$testStoreResponseMiss = $this->assertCacheMissAndReturnResponse(
218218
$query,
219219
[
@@ -225,18 +225,18 @@ public function testCachePurgedWithStoreScopeConfigChange(): void
225225
$testStoreResponseMissResult = $testStoreResponseMiss['body']['storeConfig'];
226226
$this->assertEquals($testStoreCode, $testStoreResponseMissResult['code']);
227227
$this->assertEquals($newLocale, $testStoreResponseMissResult['locale']);
228-
// Verify we obtain a cache HIT the 3rd time
229-
$testStoreResponseHit2 = $this->assertCacheHitAndReturnResponse(
228+
// Verify we obtain a cache HIT at the 3rd time
229+
$testStoreResponseHit = $this->assertCacheHitAndReturnResponse(
230230
$query,
231231
[
232232
CacheIdCalculator::CACHE_ID_HEADER => $testStoreCacheId,
233233
'Store' => $testStoreCode
234234
]
235235
);
236-
$this->assertArrayHasKey('storeConfig', $testStoreResponseHit2['body']);
237-
$testStoreResponseHit2Result = $testStoreResponseHit2['body']['storeConfig'];
238-
$this->assertEquals($testStoreCode, $testStoreResponseHit2Result['code']);
239-
$this->assertEquals($newLocale, $testStoreResponseHit2Result['locale']);
236+
$this->assertArrayHasKey('storeConfig', $testStoreResponseHit['body']);
237+
$testStoreResponseHitResult = $testStoreResponseHit['body']['storeConfig'];
238+
$this->assertEquals($testStoreCode, $testStoreResponseHitResult['code']);
239+
$this->assertEquals($newLocale, $testStoreResponseHitResult['locale']);
240240
}
241241

242242
/**

0 commit comments

Comments
 (0)