Skip to content

Commit 017fa57

Browse files
committed
ICacheSource::getCacheLifetime() requires a second parameter to calculate the expiration time of the cache
1 parent c8193e5 commit 017fa57

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

wcfsetup/install/files/lib/system/cache/source/DiskCacheSource.class.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,12 @@ private function readCache(string $filename): mixed
159159
}
160160

161161
#[\Override]
162-
public function getCacheLifetime(string $cacheName): ?int
162+
public function getExpirationTime(string $cacheName, int $maxLifetime): ?int
163163
{
164+
if ($maxLifetime === 0) {
165+
return \PHP_INT_MAX;
166+
}
167+
164168
$filename = $this->getFilename($cacheName);
165169

166170
if (!\file_exists($filename)) {
@@ -171,6 +175,6 @@ public function getCacheLifetime(string $cacheName): ?int
171175
return null;
172176
}
173177

174-
return \filemtime($filename);
178+
return \filemtime($filename) + $maxLifetime;
175179
}
176180
}

wcfsetup/install/files/lib/system/cache/source/ICacheSource.class.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public function get($cacheName, $maxLifetime);
4343
public function set($cacheName, $value, $maxLifetime);
4444

4545
/**
46-
* Returns the cache lifetime for a specific cache.
46+
* Returns the timestamp when the cache expires.
47+
* Or `null` if the cache does not exist or is empty.
48+
* `\PHP_INT_MAX` means the cache never expires.
4749
*/
48-
public function getCacheLifetime(string $cacheName): ?int;
50+
public function getExpirationTime(string $cacheName, int $maxLifetime): ?int;
4951
}

wcfsetup/install/files/lib/system/cache/source/RedisCacheSource.class.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function getRedis()
186186
}
187187

188188
#[\Override]
189-
public function getCacheLifetime(string $cacheName): ?int
189+
public function getExpirationTime(string $cacheName, int $maxLifetime): ?int
190190
{
191191
$parts = \explode('-', $cacheName, 2);
192192

@@ -197,7 +197,15 @@ public function getCacheLifetime(string $cacheName): ?int
197197
}
198198

199199
// -2 means that the key does not exist
200+
if ($ttl === -2) {
201+
return null;
202+
}
203+
200204
// -1 means that the key exists but does not have an expiration date.
201-
return $ttl > 0 ? $ttl : null;
205+
if ($ttl === -1) {
206+
return \PHP_INT_MAX;
207+
}
208+
209+
return $ttl;
202210
}
203211
}

wcfsetup/install/files/lib/system/cache/tolerant/AbstractTolerantCache.class.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ final public function rebuild(): void
103103
$newCacheData,
104104
0
105105
);
106-
107-
BackgroundQueueHandler::getInstance()->enqueueAt(
108-
[new TolerantCacheRebuildBackgroundJob(\get_class($this), $this->getProperties())],
109-
$this->nextRebuildTime()
110-
);
111106
}
112107

113108
/**
@@ -117,10 +112,15 @@ abstract protected function rebuildCacheData(): array | object;
117112

118113
final public function nextRebuildTime(): int
119114
{
120-
$cacheTime = CacheHandler::getInstance()->getCacheSource()->getCacheLifetime($this->getCacheKey());
115+
$cacheTime = CacheHandler::getInstance()->getCacheSource()->getExpirationTime(
116+
$this->getCacheKey(),
117+
$this->getLifetime()
118+
);
119+
121120
if ($cacheTime === null) {
122121
return \TIME_NOW;
123122
}
123+
124124
return $cacheTime + ($this->getLifetime() - 60);
125125
}
126126

0 commit comments

Comments
 (0)