Skip to content

Commit d2e5a19

Browse files
committed
MC-6273: Mysql url_rewrite select make on product view page 170+ times per request
- changes after CR
1 parent 885b7a9 commit d2e5a19

File tree

7 files changed

+41
-130
lines changed

7 files changed

+41
-130
lines changed

app/code/Magento/Config/App/Config/Type/System.php

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Magento\Config\App\Config\Type\System\Reader;
1515
use Magento\Framework\App\ScopeInterface;
1616
use Magento\Framework\Cache\FrontendInterface;
17-
use Magento\Framework\Cache\LockQueryInterface;
17+
use Magento\Framework\Cache\LockGuardedCacheLoader;
1818
use Magento\Framework\Lock\LockManagerInterface;
1919
use Magento\Framework\Serialize\SerializerInterface;
2020
use Magento\Store\Model\Config\Processor\Fallback;
@@ -92,7 +92,7 @@ class System implements ConfigTypeInterface
9292
private $encryptor;
9393

9494
/**
95-
* @var LockQueryInterface
95+
* @var LockGuardedCacheLoader
9696
*/
9797
private $lockQuery;
9898

@@ -108,7 +108,7 @@ class System implements ConfigTypeInterface
108108
* @param Reader|null $reader
109109
* @param Encryptor|null $encryptor
110110
* @param LockManagerInterface|null $locker
111-
* @param LockQueryInterface|null $lockQuery
111+
* @param LockGuardedCacheLoader|null $lockQuery
112112
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
113113
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
114114
*/
@@ -124,7 +124,7 @@ public function __construct(
124124
Reader $reader = null,
125125
Encryptor $encryptor = null,
126126
LockManagerInterface $locker = null,
127-
LockQueryInterface $lockQuery = null
127+
LockGuardedCacheLoader $lockQuery = null
128128
) {
129129
$this->postProcessor = $postProcessor;
130130
$this->cache = $cache;
@@ -134,7 +134,7 @@ public function __construct(
134134
$this->encryptor = $encryptor
135135
?: ObjectManager::getInstance()->get(Encryptor::class);
136136
$this->lockQuery = $lockQuery
137-
?: ObjectManager::getInstance()->get(LockQueryInterface::class);
137+
?: ObjectManager::getInstance()->get(LockGuardedCacheLoader::class);
138138
}
139139

140140
/**
@@ -227,18 +227,12 @@ private function loadAllData()
227227
}
228228
return $data;
229229
};
230-
$loadAction->bindTo($this);
231-
232-
$collectAction = \Closure::fromCallable([$this, 'readData']);
233-
$saveAction = \Closure::fromCallable([$this, 'cacheData']);
234-
$cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']);
235230

236231
return $this->lockQuery->lockedLoadData(
237232
self::$lockName,
238233
$loadAction,
239-
$collectAction,
240-
$saveAction,
241-
$cleanAction
234+
[$this, 'readData'],
235+
[$this, 'cacheData']
242236
);
243237
}
244238

@@ -258,18 +252,12 @@ private function loadDefaultScopeData($scopeType)
258252
}
259253
return $scopeData;
260254
};
261-
$loadAction->bindTo($this);
262-
263-
$collectAction = \Closure::fromCallable([$this, 'readData']);
264-
$saveAction = \Closure::fromCallable([$this, 'cacheData']);
265-
$cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']);
266255

267256
return $this->lockQuery->lockedLoadData(
268257
self::$lockName,
269258
$loadAction,
270-
$collectAction,
271-
$saveAction,
272-
$cleanAction
259+
[$this, 'readData'],
260+
[$this, 'cacheData']
273261
);
274262
}
275263

@@ -303,18 +291,12 @@ private function loadScopeData($scopeType, $scopeId)
303291

304292
return $scopeData;
305293
};
306-
$loadAction->bindTo($this);
307-
308-
$collectAction = \Closure::fromCallable([$this, 'readData']);
309-
$saveAction = \Closure::fromCallable([$this, 'cacheData']);
310-
$cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']);
311294

312295
return $this->lockQuery->lockedLoadData(
313296
self::$lockName,
314297
$loadAction,
315-
$collectAction,
316-
$saveAction,
317-
$cleanAction
298+
[$this, 'readData'],
299+
[$this, 'cacheData']
318300
);
319301
}
320302

@@ -356,16 +338,6 @@ private function cacheData(array $data)
356338
);
357339
}
358340

359-
/**
360-
* Clean cache action.
361-
*
362-
* @return void
363-
*/
364-
private function cleanCacheAction()
365-
{
366-
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]);
367-
}
368-
369341
/**
370342
* Walk nested hash map by keys from $pathParts.
371343
*
@@ -416,21 +388,6 @@ private function readData(): array
416388
public function clean()
417389
{
418390
$this->data = [];
419-
$loadAction = function () {
420-
return false;
421-
};
422-
423-
$collectAction = \Closure::fromCallable([$this, 'readData']);
424-
$saveAction = \Closure::fromCallable([$this, 'cacheData']);
425-
$cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']);
426-
427-
$this->lockQuery->lockedLoadData(
428-
self::$lockName,
429-
$loadAction,
430-
$collectAction,
431-
$saveAction,
432-
$cleanAction,
433-
true
434-
);
391+
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]);
435392
}
436393
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@
9494
</arguments>
9595
</type>
9696

97-
<virtualType name="systemConfigQueryLocker" type="Magento\Framework\Cache\Lock\Query">
97+
<virtualType name="systemConfigQueryLocker" type="Magento\Framework\Cache\LockGuardedCacheLoader">
9898
<arguments>
9999
<argument name="locker" xsi:type="object">Magento\Framework\Lock\Backend\Cache</argument>
100-
<argument name="lockTimeout" xsi:type="number">42</argument>
101-
<argument name="delayTimeout" xsi:type="number">100000</argument>
100+
<argument name="lockTimeout" xsi:type="number">42000</argument>
101+
<argument name="delayTimeout" xsi:type="number">100</argument>
102102
</arguments>
103103
</virtualType>
104104

app/etc/di.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@
129129
<preference for="Magento\Framework\Encryption\EncryptorInterface" type="Magento\Framework\Encryption\Encryptor" />
130130
<preference for="Magento\Framework\Filter\Encrypt\AdapterInterface" type="Magento\Framework\Filter\Encrypt\Basic" />
131131
<preference for="Magento\Framework\Cache\ConfigInterface" type="Magento\Framework\Cache\Config" />
132-
<preference for="Magento\Framework\Cache\LockQueryInterface" type="Magento\Framework\Cache\Lock\Query" />
133132
<preference for="Magento\Framework\View\Asset\MergeStrategyInterface" type="Magento\Framework\View\Asset\MergeStrategy\Direct" />
134133
<preference for="Magento\Framework\App\ViewInterface" type="Magento\Framework\App\View" />
135134
<preference for="Magento\Framework\Data\Collection\EntityFactoryInterface" type="Magento\Framework\Data\Collection\EntityFactory" />
@@ -1758,11 +1757,11 @@
17581757
</argument>
17591758
</arguments>
17601759
</type>
1761-
<type name="Magento\Framework\Cache\Lock\Query">
1760+
<type name="Magento\Framework\Cache\LockGuardedCacheLoader">
17621761
<arguments>
17631762
<argument name="locker" xsi:type="object">Magento\Framework\Lock\Backend\Cache</argument>
1764-
<argument name="lockTimeout" xsi:type="number">10</argument>
1765-
<argument name="delayTimeout" xsi:type="number">50000</argument>
1763+
<argument name="lockTimeout" xsi:type="number">10000</argument>
1764+
<argument name="delayTimeout" xsi:type="number">20</argument>
17661765
</arguments>
17671766
</type>
17681767
</config>

lib/internal/Magento/Framework/Cache/Lock/Query.php renamed to lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
namespace Magento\Framework\Cache\Lock;
7+
namespace Magento\Framework\Cache;
88

9-
use Magento\Framework\Cache\LockQueryInterface;
109
use Magento\Framework\Lock\LockManagerInterface;
1110

1211
/**
13-
* Default mutex for cache concurrent access.
12+
* Default mutex that provide concurrent access to cache storage.
1413
*/
15-
class Query implements LockQueryInterface
14+
class LockGuardedCacheLoader
1615
{
1716
/**
1817
* @var LockManagerInterface
@@ -22,7 +21,7 @@ class Query implements LockQueryInterface
2221
/**
2322
* Lifetime of the lock for write in cache.
2423
*
25-
* Value of the variable in seconds.
24+
* Value of the variable in milliseconds.
2625
*
2726
* @var int
2827
*/
@@ -31,7 +30,7 @@ class Query implements LockQueryInterface
3130
/**
3231
* Timeout between retrieves to load the configuration from the cache.
3332
*
34-
* Value of the variable in microseconds.
33+
* Value of the variable in milliseconds.
3534
*
3635
* @var int
3736
*/
@@ -44,8 +43,8 @@ class Query implements LockQueryInterface
4443
*/
4544
public function __construct(
4645
LockManagerInterface $locker,
47-
int $lockTimeout = 10,
48-
int $delayTimeout = 20000
46+
int $lockTimeout = 10000,
47+
int $delayTimeout = 20
4948
) {
5049
$this->locker = $locker;
5150
$this->lockTimeout = $lockTimeout;
@@ -59,35 +58,28 @@ public function lockedLoadData(
5958
string $lockName,
6059
callable $dataLoader,
6160
callable $dataCollector,
62-
callable $dataSaver,
63-
callable $dataCleaner,
64-
bool $flush = false
61+
callable $dataSaver
6562
) {
6663
$cachedData = $dataLoader(); //optimistic read
6764

6865
while ($cachedData === false && $this->locker->isLocked($lockName)) {
69-
usleep($this->delayTimeout);
66+
usleep($this->delayTimeout * 1000);
7067
$cachedData = $dataLoader();
7168
}
7269

7370
while ($cachedData === false) {
7471
try {
75-
if ($this->locker->lock($lockName, $this->lockTimeout)) {
76-
if (!$flush) {
77-
$data = $dataCollector();
78-
$dataSaver($data);
79-
$cachedData = $data;
80-
} else {
81-
$dataCleaner();
82-
$cachedData = [];
83-
}
72+
if ($this->locker->lock($lockName, $this->lockTimeout / 1000)) {
73+
$data = $dataCollector();
74+
$dataSaver($data);
75+
$cachedData = $data;
8476
}
8577
} finally {
8678
$this->locker->unlock($lockName);
8779
}
8880

8981
if ($cachedData === false) {
90-
usleep($this->delayTimeout);
82+
usleep($this->delayTimeout * 1000);
9183
$cachedData = $dataLoader();
9284
}
9385
}

lib/internal/Magento/Framework/Cache/LockQueryInterface.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/internal/Magento/Framework/View/Element/AbstractBlock.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Magento\Framework\View\Element;
88

9-
use Magento\Framework\Cache\LockQueryInterface;
9+
use Magento\Framework\Cache\LockGuardedCacheLoader;
1010
use Magento\Framework\DataObject\IdentityInterface;
1111
use Magento\Framework\App\ObjectManager;
1212

@@ -179,7 +179,7 @@ abstract class AbstractBlock extends \Magento\Framework\DataObject implements Bl
179179
protected $_cache;
180180

181181
/**
182-
* @var LockQueryInterface
182+
* @var LockGuardedCacheLoader
183183
*/
184184
private $lockQuery;
185185

@@ -188,12 +188,12 @@ abstract class AbstractBlock extends \Magento\Framework\DataObject implements Bl
188188
*
189189
* @param \Magento\Framework\View\Element\Context $context
190190
* @param array $data
191-
* @param LockQueryInterface|null $lockQuery
191+
* @param LockGuardedCacheLoader|null $lockQuery
192192
*/
193193
public function __construct(
194194
\Magento\Framework\View\Element\Context $context,
195195
array $data = [],
196-
LockQueryInterface $lockQuery = null
196+
LockGuardedCacheLoader $lockQuery = null
197197
) {
198198
$this->_request = $context->getRequest();
199199
$this->_layout = $context->getLayout();
@@ -217,7 +217,7 @@ public function __construct(
217217
unset($data['jsLayout']);
218218
}
219219
$this->lockQuery = $lockQuery
220-
?: ObjectManager::getInstance()->get(LockQueryInterface::class);
220+
?: ObjectManager::getInstance()->get(LockGuardedCacheLoader::class);
221221
parent::__construct($data);
222222
$this->_construct();
223223
}
@@ -1096,7 +1096,6 @@ protected function _loadCache()
10961096
$this->_beforeToHtml();
10971097
return $this->_toHtml();
10981098
};
1099-
$collectAction->bindTo($this);
11001099

11011100
if ($this->getCacheLifetime() === null || !$this->_cacheState->isEnabled(self::CACHE_GROUP)) {
11021101
$html = $collectAction();
@@ -1119,25 +1118,19 @@ protected function _loadCache()
11191118
}
11201119
return $cacheData;
11211120
};
1122-
$loadAction->bindTo($this);
11231121

11241122
$saveAction = function ($data) {
11251123
$this->_saveCache($data);
11261124
if ($this->hasData('translate_inline')) {
11271125
$this->inlineTranslation->resume();
11281126
}
11291127
};
1130-
$saveAction->bindTo($this);
1131-
1132-
$cleanAction = function () {
1133-
};
11341128

11351129
return $this->lockQuery->lockedLoadData(
11361130
$this->getCacheKey(),
11371131
$loadAction,
11381132
$collectAction,
1139-
$saveAction,
1140-
$cleanAction
1133+
$saveAction
11411134
);
11421135
}
11431136

0 commit comments

Comments
 (0)