Skip to content

Commit 8a4fb7f

Browse files
committed
B2B-2677: [MediaGallery]Implement data caching for GraphQL results on resolver level
- Invalidate media gallery resolver cache on import
1 parent 5376c17 commit 8a4fb7f

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Observer;
9+
10+
use Magento\Framework\Event\Observer;
11+
use Magento\CatalogGraphQl\Model\Resolver\Cache\Product\MediaGallery\ResolverCacheIdentity;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\GraphQlResolverCache\Model\Resolver\Result\Type as GraphQlResolverCache;
14+
15+
class AfterImportDataObserver implements ObserverInterface
16+
{
17+
/**
18+
* @var GraphQlResolverCache
19+
*/
20+
private $graphQlResolverCache;
21+
22+
/**
23+
* @param GraphQlResolverCache $graphQlResolverCache
24+
*/
25+
public function __construct(GraphQlResolverCache $graphQlResolverCache)
26+
{
27+
$this->graphQlResolverCache = $graphQlResolverCache;
28+
}
29+
30+
public function execute(Observer $observer)
31+
{
32+
$mediaGalleryEntriesChanged = $observer->getEvent()->getMediaGallery();
33+
34+
$productSkusToInvalidate = [];
35+
36+
foreach ($mediaGalleryEntriesChanged as $productSkus) {
37+
$productSkusToInvalidate[] = array_keys($productSkus);
38+
}
39+
40+
$productSkusToInvalidate = array_merge([], ...$productSkusToInvalidate);
41+
42+
$tags = array_map(function ($productSku) {
43+
return sprintf('%s_%s', ResolverCacheIdentity::CACHE_TAG, $productSku);
44+
}, $productSkusToInvalidate);
45+
46+
if (!empty($tags)) {
47+
$this->graphQlResolverCache->clean(
48+
\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
49+
$tags
50+
);
51+
}
52+
}
53+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
9+
<event name="catalog_product_import_bunch_save_after">
10+
<observer name="invalidate_media_gallery_resolver_cache" instance="Magento\CatalogGraphQl\Observer\AfterImportDataObserver"/>
11+
</event>
12+
</config>

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ protected function _saveProducts()
16941694
$this->_saveProductAttributes($attributes);
16951695
$this->_eventManager->dispatch(
16961696
'catalog_product_import_bunch_save_after',
1697-
['adapter' => $this, 'bunch' => $bunch]
1697+
['adapter' => $this, 'bunch' => $bunch, 'media_gallery' => $mediaGallery]
16981698
);
16991699
}
17001700
return $this;

0 commit comments

Comments
 (0)