mirror of
https://github.com/nextcloud/server.git
synced 2026-09-29 14:14:37 +02:00
PHP 8.3 allows class constants to declare a type. Adopt it where doing so cannot break third-party apps. Typing an inheritable constant is a hard BC break: a subclass that redeclares it untyped fails to load with "Type of C::FOO must be compatible with P::FOO of type string". Changes are therefore limited to constants that cannot be redeclared by a subclass: - private const (not inherited) - final public/protected const - constants declared in a final class or an enum Interface constants, trait constants, and public/protected constants in non-final (including abstract) classes are left untyped, as is all of lib/public (OCP) and lib/unstable (NCU). One review-requested exception: the public OBJECT_PREFIX/OBJECT_SUFFIX constants of the app-internal CalDAV import helpers (TextImporter, XmlImporter) are typed as well; these classes are not public API and have no subclasses. Only string, int and array are used. float is avoided because it would silently coerce an int literal and change === comparisons. No constant name or value is modified: every changed line adds only the type token, so runtime behaviour is unchanged. Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
273 lines
7.2 KiB
PHP
273 lines
7.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
namespace OC\Memcache;
|
|
|
|
use OC\RedisFactory;
|
|
use OCP\IMemcacheTTL;
|
|
use OCP\Server;
|
|
|
|
class Redis extends Cache implements IMemcacheTTL {
|
|
/** name => [script, sha1] */
|
|
public const LUA_SCRIPTS = [
|
|
'dec' => [
|
|
'if redis.call("exists", KEYS[1]) == 1 then return redis.call("decrby", KEYS[1], ARGV[1]) else return "NEX" end',
|
|
'720b40cb66cef1579f2ef16ec69b3da8c85510e9',
|
|
],
|
|
'cas' => [
|
|
'if redis.call("get", KEYS[1]) == ARGV[1] then redis.call("set", KEYS[1], ARGV[2]) return 1 else return 0 end',
|
|
'94eac401502554c02b811e3199baddde62d976d4',
|
|
],
|
|
'cad' => [
|
|
'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 int MAX_TTL = 30 * 24 * 60 * 60; // 1 month
|
|
|
|
/** Number of keys to request per SCAN iteration in {@see self::clear()} (only a hint to Redis) */
|
|
private const int SCAN_COUNT = 1000;
|
|
|
|
private \Redis|\RedisCluster|null $cache = null;
|
|
|
|
public function __construct($prefix = '', string $logFile = '') {
|
|
parent::__construct($prefix);
|
|
}
|
|
|
|
/**
|
|
* @throws \Exception
|
|
*/
|
|
public function getCache(): \Redis|\RedisCluster {
|
|
if ($this->cache === null) {
|
|
$this->cache = Server::get(RedisFactory::class)->getInstance();
|
|
}
|
|
return $this->cache;
|
|
}
|
|
|
|
#[\Override]
|
|
public function get($key) {
|
|
$result = $this->getCache()->get($this->getPrefix() . $key);
|
|
if ($result === false) {
|
|
return null;
|
|
}
|
|
|
|
return self::decodeValue($result);
|
|
}
|
|
|
|
#[\Override]
|
|
public function set($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);
|
|
return $this->getCache()->setex($this->getPrefix() . $key, $ttl, $value);
|
|
}
|
|
|
|
#[\Override]
|
|
public function hasKey($key) {
|
|
return (bool)$this->getCache()->exists($this->getPrefix() . $key);
|
|
}
|
|
|
|
#[\Override]
|
|
public function remove($key) {
|
|
if ($this->getCache()->unlink($this->getPrefix() . $key)) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#[\Override]
|
|
public function clear($prefix = '') {
|
|
$pattern = $this->getPrefix() . $prefix . '*';
|
|
$cache = $this->getCache();
|
|
|
|
// Iterate with SCAN and remove with UNLINK rather than KEYS + DEL:
|
|
// KEYS walks the whole keyspace and blocks the server, while a
|
|
// multi-key DEL/UNLINK is not cluster-safe (keys spanning hash slots
|
|
// raise a CROSSSLOT error). SCAN is non-blocking and UNLINK reclaims
|
|
// memory in the background.
|
|
if ($cache instanceof \RedisCluster) {
|
|
// On a cluster SCAN must be run against each master node, and keys
|
|
// are unlinked one at a time so each command stays within a slot.
|
|
foreach ($cache->_masters() as $master) {
|
|
$iterator = null;
|
|
do {
|
|
/** @psalm-suppress NullArgument, PossiblyNullArgument the SCAN cursor must start as null (the phpredis stub types it as int) */
|
|
$keys = $cache->scan($iterator, $master, $pattern, self::SCAN_COUNT);
|
|
if ($keys === false) {
|
|
break;
|
|
}
|
|
foreach ($keys as $key) {
|
|
$cache->unlink($key);
|
|
}
|
|
} while ($iterator > 0);
|
|
}
|
|
} else {
|
|
$iterator = null;
|
|
do {
|
|
/** @psalm-suppress NullArgument, PossiblyNullArgument the SCAN cursor must start as null (the phpredis stub types it as int) */
|
|
$keys = $cache->scan($iterator, $pattern, self::SCAN_COUNT);
|
|
if ($keys === false) {
|
|
break;
|
|
}
|
|
if ($keys !== []) {
|
|
$cache->unlink($keys);
|
|
}
|
|
} while ($iterator > 0);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Set a value in the cache if it's not already stored
|
|
*
|
|
* @param string $key
|
|
* @param mixed $value
|
|
* @param int $ttl Time To Live in seconds. Defaults to 60*60*24
|
|
* @return bool
|
|
*/
|
|
#[\Override]
|
|
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'];
|
|
$args['ex'] = $ttl;
|
|
|
|
return $this->getCache()->set($this->getPrefix() . $key, $value, $args);
|
|
}
|
|
|
|
/**
|
|
* Increase a stored number
|
|
*
|
|
* @param string $key
|
|
* @param int $step
|
|
* @return int | bool
|
|
*/
|
|
#[\Override]
|
|
public function inc($key, $step = 1) {
|
|
return $this->getCache()->incrBy($this->getPrefix() . $key, $step);
|
|
}
|
|
|
|
/**
|
|
* Decrease a stored number
|
|
*
|
|
* @param string $key
|
|
* @param int $step
|
|
* @return int | bool
|
|
*/
|
|
#[\Override]
|
|
public function dec($key, $step = 1) {
|
|
$res = $this->evalLua('dec', [$key], [$step]);
|
|
return ($res === 'NEX') ? false : $res;
|
|
}
|
|
|
|
/**
|
|
* Compare and set
|
|
*
|
|
* @param string $key
|
|
* @param mixed $old
|
|
* @param mixed $new
|
|
* @return bool
|
|
*/
|
|
#[\Override]
|
|
public function cas($key, $old, $new) {
|
|
$old = self::encodeValue($old);
|
|
$new = self::encodeValue($new);
|
|
|
|
return $this->evalLua('cas', [$key], [$old, $new]) > 0;
|
|
}
|
|
|
|
/**
|
|
* Compare and delete
|
|
*
|
|
* @param string $key
|
|
* @param mixed $old
|
|
* @return bool
|
|
*/
|
|
#[\Override]
|
|
public function cad($key, $old) {
|
|
$old = self::encodeValue($old);
|
|
|
|
return $this->evalLua('cad', [$key], [$old]) > 0;
|
|
}
|
|
|
|
#[\Override]
|
|
public function ncad(string $key, mixed $old): bool {
|
|
$old = self::encodeValue($old);
|
|
|
|
return $this->evalLua('ncad', [$key], [$old]) > 0;
|
|
}
|
|
|
|
#[\Override]
|
|
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);
|
|
}
|
|
|
|
#[\Override]
|
|
public function getTTL(string $key): int|false {
|
|
$ttl = $this->getCache()->ttl($this->getPrefix() . $key);
|
|
return $ttl > 0 ? (int)$ttl : false;
|
|
}
|
|
|
|
#[\Override]
|
|
public function compareSetTTL(string $key, mixed $value, int $ttl): bool {
|
|
$value = self::encodeValue($value);
|
|
|
|
return $this->evalLua('caSetTtl', [$key], [$value, $ttl]) > 0;
|
|
}
|
|
|
|
#[\Override]
|
|
public static function isAvailable(): bool {
|
|
return Server::get(RedisFactory::class)->isAvailable();
|
|
}
|
|
|
|
protected function evalLua(string $scriptName, array $keys, array $args) {
|
|
$keys = array_map(fn ($key) => $this->getPrefix() . $key, $keys);
|
|
$args = array_merge($keys, $args);
|
|
$script = self::LUA_SCRIPTS[$scriptName];
|
|
|
|
$result = $this->getCache()->evalSha($script[1], $args, count($keys));
|
|
if ($result === false) {
|
|
$result = $this->getCache()->eval($script[0], $args, count($keys));
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
protected static function encodeValue(mixed $value): string {
|
|
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);
|
|
}
|
|
}
|