Skip to content

Commit 0e81d6f

Browse files
committed
MC-33275: Stale cache implementation
1 parent dfc4f8b commit 0e81d6f

File tree

5 files changed

+58
-156
lines changed

5 files changed

+58
-156
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
namespace Magento\Framework\Cache\Backend;
88

9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Lock\LockManagerInterface;
11+
912
/**
1013
* Remote synchronized cache
1114
*
@@ -42,6 +45,11 @@ class RemoteSynchronizedCache extends \Zend_Cache_Backend implements \Zend_Cache
4245
*/
4346
private const HASH_SUFFIX = ':hash';
4447

48+
/**
49+
* @var LockManagerInterface
50+
*/
51+
private $lockManager;
52+
4553
/**
4654
* @inheritdoc
4755
*/
@@ -101,6 +109,8 @@ public function __construct(array $options = [])
101109
);
102110
}
103111
}
112+
113+
$this->lockManager = ObjectManager::getInstance()->get(LockManagerInterface::class);
104114
}
105115

106116
/**

lib/internal/Magento/Framework/Cache/Frontend/Adapter/InMemoryCache.php

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

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
namespace Magento\Framework\Cache;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\Lock\LockManagerInterface;
11+
use Magento\Framework\App\DeploymentConfig;
1012

1113
/**
1214
* Default mutex that provide concurrent access to cache storage.
@@ -55,24 +57,47 @@ class LockGuardedCacheLoader
5557
private $minimalDelayTimeout;
5658

5759
/**
60+
* @var DeploymentConfig
61+
*/
62+
private $deploymentConfig;
63+
64+
/**
65+
* Option that allows to switch off blocking for parallel generation.
66+
*
67+
* @var string
68+
*/
69+
private const CONFIG_PATH_ALLOW_BLOCKING_GENERATION = 'cache/allow_parallel_generation';
70+
71+
/**
72+
* Config value of parallel generation.
73+
*
74+
* @var bool
75+
*/
76+
private $allowParallelGenerationConfigValue;
77+
78+
/**
79+
* LockGuardedCacheLoader constructor.
5880
* @param LockManagerInterface $locker
5981
* @param int $lockTimeout
6082
* @param int $delayTimeout
6183
* @param int $loadTimeout
6284
* @param int $minimalDelayTimeout
85+
* @param DeploymentConfig|null $deploymentConfig
6386
*/
6487
public function __construct(
6588
LockManagerInterface $locker,
6689
int $lockTimeout = 10000,
6790
int $delayTimeout = 20,
6891
int $loadTimeout = 10000,
69-
int $minimalDelayTimeout = 5
92+
int $minimalDelayTimeout = 5,
93+
DeploymentConfig $deploymentConfig = null
7094
) {
7195
$this->locker = $locker;
7296
$this->lockTimeout = $lockTimeout;
7397
$this->delayTimeout = $delayTimeout;
7498
$this->loadTimeout = $loadTimeout;
7599
$this->minimalDelayTimeout = $minimalDelayTimeout;
100+
$this->deploymentConfig = $deploymentConfig ?? ObjectManager::getInstance()->get(DeploymentConfig::class);
76101
}
77102

78103
/**
@@ -93,6 +118,12 @@ public function lockedLoadData(
93118
$cachedData = $dataLoader(); //optimistic read
94119
$deadline = microtime(true) + $this->loadTimeout / 100;
95120

121+
if (empty($this->allowParallelGenerationConfigValue)) {
122+
$this->allowParallelGenerationConfigValue = $this
123+
->deploymentConfig
124+
->getConfigData([self::CONFIG_PATH_ALLOW_BLOCKING_GENERATION]);
125+
}
126+
96127
while ($cachedData === false) {
97128
if ($deadline <= microtime(true)) {
98129
return $dataCollector();
@@ -106,6 +137,8 @@ public function lockedLoadData(
106137
} finally {
107138
$this->locker->unlock($lockName);
108139
}
140+
} elseif ($this->allowParallelGenerationConfigValue === true) {
141+
return $dataCollector();
109142
}
110143

111144
if ($cachedData === false) {

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

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,51 +1107,35 @@ protected function _loadCache()
11071107
}
11081108

11091109
$this->_beforeToHtml();
1110-
$data = $this->_toHtml();
1110+
return $this->_toHtml();
1111+
};
11111112

1113+
if ($this->getCacheLifetime() === null || !$this->_cacheState->isEnabled(self::CACHE_GROUP)) {
1114+
$html = $collectAction();
11121115
if ($this->hasData('translate_inline')) {
11131116
$this->inlineTranslation->resume();
11141117
}
1115-
1116-
return $data;
1117-
};
1118-
1119-
if ($this->getCacheLifetime() === null || !$this->_cacheState->isEnabled(self::CACHE_GROUP)) {
1120-
return $collectAction();
1118+
return $html;
11211119
}
1122-
11231120
$loadAction = function () {
11241121
return $this->_cache->load($this->getCacheKey());
11251122
};
11261123

11271124
$saveAction = function ($data) {
11281125
$this->_saveCache($data);
1129-
return $data;
1126+
if ($this->hasData('translate_inline')) {
1127+
$this->inlineTranslation->resume();
1128+
}
11301129
};
11311130

1132-
return (string)$this->lockQuery->nonBlockingLockedLoadData(
1133-
$this->shortenLockName($this->getCacheKey()),
1131+
return (string)$this->lockQuery->lockedLoadData(
1132+
$this->getCacheKey(),
11341133
$loadAction,
11351134
$collectAction,
11361135
$saveAction
11371136
);
11381137
}
11391138

1140-
/**
1141-
* Shortens lock name in case plugin increased cache key size out of sha1 length
1142-
*
1143-
* @param string $cacheKey
1144-
* @return string
1145-
*/
1146-
private function shortenLockName(string $cacheKey)
1147-
{
1148-
if (strlen(self::CACHE_KEY_PREFIX) + 40 < strlen($cacheKey)) {
1149-
return self::CACHE_KEY_PREFIX . sha1($cacheKey);
1150-
}
1151-
1152-
return $cacheKey;
1153-
}
1154-
11551139
/**
11561140
* Save block content to cache storage
11571141
*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Cache implements ConfigOptionsListInterface
3939
const CONFIG_PATH_CACHE_BACKEND_COMPRESS_DATA = 'cache/frontend/default/backend_options/compress_data';
4040
const CONFIG_PATH_CACHE_BACKEND_COMPRESSION_LIB = 'cache/frontend/default/backend_options/compression_lib';
4141
const CONFIG_PATH_CACHE_ID_PREFIX = 'cache/frontend/default/id_prefix';
42+
const CONFIG_PATH_ALLOW_BLOCKING_GENERATION = 'cache/allow_parallel_generation';
43+
4244

4345
/**
4446
* @var array
@@ -165,6 +167,8 @@ public function createConfig(array $options, DeploymentConfig $deploymentConfig)
165167
}
166168
}
167169

170+
$configData->set(self::CONFIG_PATH_ALLOW_BLOCKING_GENERATION, false);
171+
168172
foreach ($this->inputKeyToConfigPathMap as $inputKey => $configPath) {
169173
if (isset($options[$inputKey])) {
170174
$configData->set($configPath, $options[$inputKey]);

0 commit comments

Comments
 (0)