Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 0c2f1d3

Browse files
authored
ENGCOM-3804: Fix Swagger caching issue #31
2 parents 2952946 + 3f9ec69 commit 0c2f1d3

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\WebapiAsync\Plugin\Cache;
10+
11+
use Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor;
12+
use Magento\Framework\Webapi\Rest\Request;
13+
14+
/**
15+
* Class Webapi
16+
*/
17+
class Webapi
18+
{
19+
/**
20+
* Cache key for Async Routes
21+
*/
22+
const ASYNC_ROUTES_CONFIG_CACHE_ID = 'async-routes-services-config';
23+
24+
/**
25+
* @var AsynchronousSchemaRequestProcessor
26+
*/
27+
private $asynchronousSchemaRequestProcessor;
28+
29+
/**
30+
* @var \Magento\Framework\Webapi\Rest\Request
31+
*/
32+
private $request;
33+
34+
/**
35+
* ServiceMetadata constructor.
36+
*
37+
* @param Request $request
38+
* @param AsynchronousSchemaRequestProcessor $asynchronousSchemaRequestProcessor
39+
*/
40+
public function __construct(
41+
\Magento\Framework\Webapi\Rest\Request $request,
42+
AsynchronousSchemaRequestProcessor $asynchronousSchemaRequestProcessor
43+
) {
44+
$this->request = $request;
45+
$this->asynchronousSchemaRequestProcessor = $asynchronousSchemaRequestProcessor;
46+
}
47+
48+
/**
49+
* Change identifier in case if Async request before cache load
50+
*
51+
* @param \Magento\Webapi\Model\Cache\Type\Webapi $subject
52+
* @param string $identifier
53+
* @return null|string
54+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
55+
*/
56+
public function beforeLoad(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier)
57+
{
58+
if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)
59+
&& $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) {
60+
return self::ASYNC_ROUTES_CONFIG_CACHE_ID;
61+
}
62+
return null;
63+
}
64+
65+
/**
66+
* Change identifier in case if Async request before cache save
67+
*
68+
* @param \Magento\Webapi\Model\Cache\Type\Webapi $subject
69+
* @param string $data
70+
* @param string $identifier
71+
* @param array $tags
72+
* @param int|bool|null $lifeTime
73+
* @return array|null
74+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
75+
*/
76+
public function beforeSave(
77+
\Magento\Webapi\Model\Cache\Type\Webapi $subject,
78+
$data,
79+
$identifier,
80+
array $tags = [],
81+
$lifeTime = null
82+
) {
83+
if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)
84+
&& $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) {
85+
return [$data, self::ASYNC_ROUTES_CONFIG_CACHE_ID, $tags, $lifeTime];
86+
}
87+
return null;
88+
}
89+
90+
/**
91+
* Change identifier in case if Async request before remove cache
92+
*
93+
* @param \Magento\Webapi\Model\Cache\Type\Webapi $subject
94+
* @param string $identifier
95+
* @return null|string
96+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
97+
*/
98+
public function beforeRemove(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier)
99+
{
100+
if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)
101+
&& $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) {
102+
return self::ASYNC_ROUTES_CONFIG_CACHE_ID;
103+
}
104+
return null;
105+
}
106+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<type name="Magento\Webapi\Model\ServiceMetadata">
1111
<plugin name="webapiServiceMetadataAsync" type="Magento\WebapiAsync\Plugin\ServiceMetadata" />
1212
</type>
13+
<type name="Magento\Webapi\Model\Cache\Type\Webapi">
14+
<plugin name="webapiCacheAsync" type="Magento\WebapiAsync\Plugin\Cache\Webapi" />
15+
</type>
1316
<virtualType name="Magento\WebapiAsync\Model\VirtualType\Rest\Config" type="Magento\Webapi\Model\Rest\Config">
1417
<arguments>
1518
<argument name="config" xsi:type="object">Magento\WebapiAsync\Model\BulkServiceConfig</argument>

0 commit comments

Comments
 (0)