Skip to content

Commit b7d0b6e

Browse files
committed
Ensure that Eager caches are only created once at runtime for the same parameters
1 parent 5b956a2 commit b7d0b6e

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

wcfsetup/install/files/lib/system/cache/builder/AbstractLegacyCacheBuilder.class.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace wcf\system\cache\builder;
44

5+
use wcf\system\cache\CacheHandler;
56
use wcf\system\exception\SystemException;
67
use wcf\system\SingletonFactory;
78

@@ -17,10 +18,18 @@
1718
*/
1819
abstract class AbstractLegacyCacheBuilder extends SingletonFactory implements ICacheBuilder
1920
{
21+
protected array $cache = [];
22+
2023
#[\Override]
2124
public function getData(array $parameters = [], $arrayIndex = '')
2225
{
23-
$cache = $this->rebuild($parameters);
26+
$index = CacheHandler::getInstance()->getCacheIndex($parameters);
27+
if (isset($this->cache[$index])) {
28+
$cache = $this->cache[$index];
29+
} else {
30+
$cache = $this->rebuild($parameters);
31+
$this->cache[$index] = $cache;
32+
}
2433

2534
if (!empty($arrayIndex)) {
2635
if (!\array_key_exists($arrayIndex, $cache)) {

wcfsetup/install/files/lib/system/cache/eager/AbstractEagerCache.class.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,15 @@ private function getCacheKey(): string
8080
final public function rebuild(): void
8181
{
8282
$key = $this->getCacheKey();
83-
AbstractEagerCache::$caches[$key] = $this->getCacheData();
84-
CacheHandler::getInstance()->getCacheSource()->set($key, AbstractEagerCache::$caches[$key], 0);
83+
$newCacheData = $this->getCacheData();
84+
85+
// The existing cache must not be overwritten, otherwise this can cause errors at runtime.
86+
// The new data will be available at the next request.
87+
if (!\array_key_exists($key, AbstractEagerCache::$caches)) {
88+
AbstractEagerCache::$caches[$key] = $newCacheData;
89+
}
90+
91+
CacheHandler::getInstance()->getCacheSource()->set($key, $newCacheData, 0);
8592
}
8693

8794
/**

0 commit comments

Comments
 (0)