Skip to content

Commit 3081025

Browse files
authored
Merge pull request #6315 from WoltLab/redis-hset-suffix
Add a suffix to prevent key collisions with parameterized caches
2 parents bf4ab51 + 8478c53 commit 3081025

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ public function flush($cacheName, $useWildcard)
4646
if (isset($parts[1])) {
4747
if ($useWildcard) {
4848
// delete the complete hashset
49-
$this->redis->del($this->getCacheName($parts[0]));
49+
$this->redis->del(
50+
$this->getCacheName($parts[0], true),
51+
$this->getCacheName($parts[0]),
52+
);
5053
} else {
5154
// delete the specified key from the hashset
52-
$this->redis->hDel($this->getCacheName($parts[0]), $parts[1]);
55+
$this->redis->hDel($this->getCacheName($parts[0], true), $parts[1]);
5356
}
5457
} else {
5558
$this->redis->del($this->getCacheName($cacheName));
@@ -77,7 +80,7 @@ public function get($cacheName, $maxLifetime)
7780
$parts = \explode('-', $cacheName, 2);
7881

7982
if (isset($parts[1])) {
80-
$value = $this->redis->hGet($this->getCacheName($parts[0]), $parts[1]);
83+
$value = $this->redis->hGet($this->getCacheName($parts[0], true), $parts[1]);
8184
} else {
8285
$value = $this->redis->get($this->getCacheName($cacheName));
8386
}
@@ -123,7 +126,7 @@ public function set($cacheName, $value, $maxLifetime)
123126

124127
// check if entry is parameterized
125128
if (isset($parts[1])) {
126-
$key = $this->getCacheName($parts[0]);
129+
$key = $this->getCacheName($parts[0], true);
127130

128131
// save parameterized cache entries as field in a hashset
129132
// saving in a hashset is safe as the smallest lifetime of its fields is set as TTL for the whole hashset
@@ -148,7 +151,7 @@ public function set($cacheName, $value, $maxLifetime)
148151
* @param string $cacheName
149152
* @return string
150153
*/
151-
protected function getCacheName($cacheName)
154+
protected function getCacheName($cacheName, bool $parameterized = false)
152155
{
153156
$flush = $this->redis->get('cache:_flush');
154157

@@ -160,6 +163,10 @@ protected function getCacheName($cacheName)
160163
$flush = $this->redis->get('cache:_flush');
161164
}
162165

166+
if ($parameterized) {
167+
$cacheName .= ':hset';
168+
}
169+
163170
return 'cache:' . $flush . ':' . $cacheName;
164171
}
165172

0 commit comments

Comments
 (0)