Skip to content

Commit f4d6b83

Browse files
committed
MC-33275: Stale cache implementation
1 parent 832bfdf commit f4d6b83

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

dev/tests/integration/testsuite/Magento/Framework/Lock/Backend/CacheTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Framework\Lock\Backend;
79

810
use Magento\Framework\Lock\Backend\Cache;
911

12+
/**
13+
* \Magento\Framework\Lock\Backend\Cache test case.
14+
*/
1015
class CacheTest extends \PHPUnit\Framework\TestCase
1116
{
1217
/**

dev/tests/integration/testsuite/Magento/Framework/Lock/Backend/DatabaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\App\DeploymentConfig;
1212

1313
/**
14-
* \Magento\Framework\Lock\Backend\Database test case
14+
* \Magento\Framework\Lock\Backend\Database test case.
1515
*/
1616
class DatabaseTest extends \PHPUnit\Framework\TestCase
1717
{

lib/internal/Magento/Framework/Cache/Backend/RemoteSynchronizedCache.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class RemoteSynchronizedCache extends \Zend_Cache_Backend implements \Zend_Cache
4646
*/
4747
private const HASH_SUFFIX = ':hash';
4848

49+
/**
50+
* Prefix for locks in case stale cache is used.
51+
*/
52+
private const REMOTE_SYNC_LOCK_PREFIX = 'rsl::';
53+
4954

5055
/**
5156
* @inheritdoc
@@ -67,7 +72,7 @@ class RemoteSynchronizedCache extends \Zend_Cache_Backend implements \Zend_Cache
6772
*
6873
* @var array
6974
*/
70-
private $lockArray = [];
75+
private $lockList = [];
7176

7277
/**
7378
* Sign for locks, helps to avoid removing a lock that was created by another client
@@ -128,7 +133,6 @@ public function __construct(array $options = [])
128133
}
129134

130135
$this->lockSign = $this->generateLockSign();
131-
$this->notifier = ObjectManager::getInstance()->get(CompositeStaleCacheNotifier::class);
132136
}
133137

134138
/**
@@ -204,7 +208,7 @@ public function load($id, $doNotTestCacheValidity = false)
204208
return false;
205209
}
206210
} else {
207-
if ($versionCheckFailed = $this->getDataVersion($localData) !== $this->loadRemoteDataVersion($id)) {
211+
if ($versionCheckFailed = ($this->getDataVersion($localData) !== $this->loadRemoteDataVersion($id))) {
208212
$remoteData = $this->remote->load($id);
209213

210214
if (!$this->_options['use_stale_cache']) {
@@ -220,6 +224,8 @@ public function load($id, $doNotTestCacheValidity = false)
220224
if ($this->lock($id)) {
221225
return false;
222226
} else {
227+
$this->notifier = $this->notifier ??
228+
ObjectManager::getInstance()->get(CompositeStaleCacheNotifier::class);
223229
$this->notifier->cacheLoaderIsUsingStaleCache();
224230
}
225231
}
@@ -355,7 +361,7 @@ public function getCapabilities()
355361
*/
356362
private function lock(string $id): bool
357363
{
358-
$this->lockArray[$id] = microtime(true);
364+
$this->lockList[$id] = microtime(true);
359365

360366
$data = $this->remote->load($this->getLockName($id));
361367

@@ -382,8 +388,8 @@ private function lock(string $id): bool
382388
*/
383389
private function unlock(string $id): bool
384390
{
385-
if (isset($this->lockArray[$id])) {
386-
unset($this->lockArray[$id]);
391+
if (isset($this->lockList[$id])) {
392+
unset($this->lockList[$id]);
387393
}
388394

389395
$data = $this->remote->load($this->getLockName($id));
@@ -408,7 +414,7 @@ private function unlock(string $id): bool
408414
*/
409415
private function getLockName($id): string
410416
{
411-
return 'REMOTE_SYNC_LOCK_' . $id;
417+
return self::REMOTE_SYNC_LOCK_PREFIX . $id;
412418
}
413419

414420
/**
@@ -418,7 +424,7 @@ private function getLockName($id): string
418424
*/
419425
private function unlockAll()
420426
{
421-
foreach ($this->lockArray as $id => $ttl) {
427+
foreach ($this->lockList as $id => $ttl) {
422428
$this->unlock($id);
423429
}
424430
}
@@ -440,7 +446,7 @@ public function __destruct()
440446
*/
441447
private function generateLockSign()
442448
{
443-
$sign = implode(
449+
$sign = \implode(
444450
'-',
445451
[
446452
\getmypid(), \crc32(\gethostname())

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class LockGuardedCacheLoader
6666
*
6767
* @var string
6868
*/
69-
private const CONFIG_PATH_ALLOW_PARALLEL_CACHE_GENERATION = 'cache/allow_parallel_generation';
69+
private const CONFIG_NAME_ALLOW_PARALLEL_CACHE_GENERATION = 'allow_parallel_generation';
7070

7171
/**
7272
* Config value of parallel generation.
@@ -119,9 +119,11 @@ public function lockedLoadData(
119119
$deadline = microtime(true) + $this->loadTimeout / 100;
120120

121121
if (empty($this->allowParallelGenerationConfigValue)) {
122-
$this->allowParallelGenerationConfigValue = $this
122+
$cacheConfig = $this
123123
->deploymentConfig
124-
->getConfigData(self::CONFIG_PATH_ALLOW_PARALLEL_CACHE_GENERATION);
124+
->getConfigData('cache');
125+
$this->allowParallelGenerationConfigValue = $cacheConfig[self::CONFIG_NAME_ALLOW_PARALLEL_CACHE_GENERATION]
126+
?? false;
125127
}
126128

127129
while ($cachedData === false) {

setup/src/Magento/Setup/Model/ConfigOptionsList/Cache.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ public function createConfig(array $options, DeploymentConfig $deploymentConfig)
176176
}
177177
}
178178

179-
$configData->set(self::CONFIG_PATH_ALLOW_PARALLEL_CACHE_GENERATION, false);
180-
181179
foreach ($this->inputKeyToConfigPathMap as $inputKey => $configPath) {
182180
if (isset($options[$inputKey])) {
183181
$configData->set($configPath, $options[$inputKey]);

0 commit comments

Comments
 (0)