diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2024-02-23 18:37:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-23 18:37:15 +0100 |
commit | 18434d1ef314016fb287e764cfa1de8b74dfd0f4 (patch) | |
tree | 4e1003b7a8605b7be6745a2945fd4264963b2d9a | |
parent | 37e15612de4454db4e951d6f6aabe94bdcb6201f (diff) | |
parent | cc3c7c24a1fce57288212e3e56ff7c5ff8193c64 (diff) | |
download | nextcloud-server-18434d1ef314016fb287e764cfa1de8b74dfd0f4.tar.gz nextcloud-server-18434d1ef314016fb287e764cfa1de8b74dfd0f4.zip |
Merge pull request #38562 from nextcloud/redis-default-ttl
-rw-r--r-- | lib/private/Memcache/Redis.php | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/lib/private/Memcache/Redis.php b/lib/private/Memcache/Redis.php index 6634f325005..5e9554eb94e 100644 --- a/lib/private/Memcache/Redis.php +++ b/lib/private/Memcache/Redis.php @@ -52,6 +52,9 @@ class Redis extends Cache implements IMemcacheTTL { ], ]; + private const DEFAULT_TTL = 24 * 60 * 60; // 1 day + private const MAX_TTL = 30 * 24 * 60 * 60; // 1 month + /** * @var \Redis|\RedisCluster $cache */ @@ -83,11 +86,12 @@ class Redis extends Cache implements IMemcacheTTL { public function set($key, $value, $ttl = 0) { $value = self::encodeValue($value); - if ($ttl > 0) { - return $this->getCache()->setex($this->getPrefix() . $key, $ttl, $value); - } else { - return $this->getCache()->set($this->getPrefix() . $key, $value); + if ($ttl === 0) { + // having infinite TTL can lead to leaked keys as the prefix changes with version upgrades + $ttl = self::DEFAULT_TTL; } + $ttl = min($ttl, self::MAX_TTL); + return $this->getCache()->setex($this->getPrefix() . $key, $ttl, $value); } public function hasKey($key) { @@ -121,11 +125,14 @@ class Redis extends Cache implements IMemcacheTTL { */ public function add($key, $value, $ttl = 0) { $value = self::encodeValue($value); + if ($ttl === 0) { + // having infinite TTL can lead to leaked keys as the prefix changes with version upgrades + $ttl = self::DEFAULT_TTL; + } + $ttl = min($ttl, self::MAX_TTL); $args = ['nx']; - if ($ttl !== 0 && is_int($ttl)) { - $args['ex'] = $ttl; - } + $args['ex'] = $ttl; return $this->getCache()->set($this->getPrefix() . $key, $value, $args); } @@ -182,6 +189,11 @@ class Redis extends Cache implements IMemcacheTTL { } public function setTTL($key, $ttl) { + if ($ttl === 0) { + // having infinite TTL can lead to leaked keys as the prefix changes with version upgrades + $ttl = self::DEFAULT_TTL; + } + $ttl = min($ttl, self::MAX_TTL); $this->getCache()->expire($this->getPrefix() . $key, $ttl); } |