diff options
Diffstat (limited to 'lib/private/Memcache/Redis.php')
-rw-r--r-- | lib/private/Memcache/Redis.php | 75 |
1 files changed, 37 insertions, 38 deletions
diff --git a/lib/private/Memcache/Redis.php b/lib/private/Memcache/Redis.php index 6634f325005..f8c51570c4f 100644 --- a/lib/private/Memcache/Redis.php +++ b/lib/private/Memcache/Redis.php @@ -1,31 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Stefan Weil <sw@weilnetz.de> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Memcache; @@ -46,12 +24,18 @@ class Redis extends Cache implements IMemcacheTTL { 'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end', 'cf0e94b2e9ffc7e04395cf88f7583fc309985910', ], + 'ncad' => [ + 'if redis.call("get", KEYS[1]) ~= ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end', + '75526f8048b13ce94a41b58eee59c664b4990ab2', + ], 'caSetTtl' => [ 'if redis.call("get", KEYS[1]) == ARGV[1] then redis.call("expire", KEYS[1], ARGV[2]) return 1 else return 0 end', 'fa4acbc946d23ef41d7d3910880b60e6e4972d72', ], ]; + private const MAX_TTL = 30 * 24 * 60 * 60; // 1 month + /** * @var \Redis|\RedisCluster $cache */ @@ -67,7 +51,7 @@ class Redis extends Cache implements IMemcacheTTL { */ public function getCache() { if (is_null(self::$cache)) { - self::$cache = \OC::$server->getGetRedisFactory()->getInstance(); + self::$cache = \OC::$server->get('RedisFactory')->getInstance(); } return self::$cache; } @@ -83,11 +67,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 +106,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); } @@ -181,7 +169,18 @@ class Redis extends Cache implements IMemcacheTTL { return $this->evalLua('cad', [$key], [$old]) > 0; } + public function ncad(string $key, mixed $old): bool { + $old = self::encodeValue($old); + + return $this->evalLua('ncad', [$key], [$old]) > 0; + } + 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); } @@ -197,7 +196,7 @@ class Redis extends Cache implements IMemcacheTTL { } public static function isAvailable(): bool { - return \OC::$server->getGetRedisFactory()->isAvailable(); + return \OC::$server->get('RedisFactory')->isAvailable(); } protected function evalLua(string $scriptName, array $keys, array $args) { @@ -206,7 +205,7 @@ class Redis extends Cache implements IMemcacheTTL { $script = self::LUA_SCRIPTS[$scriptName]; $result = $this->getCache()->evalSha($script[1], $args, count($keys)); - if (false === $result) { + if ($result === false) { $result = $this->getCache()->eval($script[0], $args, count($keys)); } @@ -214,10 +213,10 @@ class Redis extends Cache implements IMemcacheTTL { } protected static function encodeValue(mixed $value): string { - return is_int($value) ? (string) $value : json_encode($value); + return is_int($value) ? (string)$value : json_encode($value); } protected static function decodeValue(string $value): mixed { - return is_numeric($value) ? (int) $value : json_decode($value, true); + return is_numeric($value) ? (int)$value : json_decode($value, true); } } |