Merge pull request #54641 from nextcloud/backport/52880/stable31

[stable31] Add commands to set/get/delete/clear the distributed memcache
backport/53752/stable31
yemkareems 2025-08-27 19:30:38 +07:00 committed by GitHub
commit 5c281401a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 355 additions and 6 deletions

@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Config\System;
class CastHelper {
/**
* @return array{value: mixed, readable-value: string}
*/
public function castValue(?string $value, string $type): array {
switch ($type) {
case 'integer':
case 'int':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (int)$value,
'readable-value' => 'integer ' . (int)$value,
];
case 'double':
case 'float':
if (!is_numeric($value)) {
throw new \InvalidArgumentException('Non-numeric value specified');
}
return [
'value' => (float)$value,
'readable-value' => 'double ' . (float)$value,
];
case 'boolean':
case 'bool':
$value = strtolower($value);
return match ($value) {
'true' => [
'value' => true,
'readable-value' => 'boolean ' . $value,
],
'false' => [
'value' => false,
'readable-value' => 'boolean ' . $value,
],
default => throw new \InvalidArgumentException('Unable to parse value as boolean'),
};
case 'null':
return [
'value' => null,
'readable-value' => 'null',
];
case 'string':
$value = (string)$value;
return [
'value' => $value,
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
];
case 'json':
$value = json_decode($value, true);
return [
'value' => $value,
'readable-value' => 'json ' . json_encode($value),
];
default:
throw new \InvalidArgumentException('Invalid type');
}
}
}

@ -17,6 +17,7 @@ use Symfony\Component\Console\Output\OutputInterface;
class SetConfig extends Base {
public function __construct(
SystemConfig $systemConfig,
private CastHelper $castHelper,
) {
parent::__construct($systemConfig);
}
@ -57,7 +58,7 @@ class SetConfig extends Base {
protected function execute(InputInterface $input, OutputInterface $output): int {
$configNames = $input->getArgument('name');
$configName = $configNames[0];
$configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
$configValue = $this->castHelper->castValue($input->getOption('value'), $input->getOption('type'));
$updateOnly = $input->getOption('update-only');
if (count($configNames) > 1) {

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Memcache;
use OC\Core\Command\Base;
use OCP\ICacheFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DistributedClear extends Base {
public function __construct(
protected ICacheFactory $cacheFactory,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('memcache:distributed:clear')
->setDescription('Clear values from the distributed memcache')
->addOption('prefix', null, InputOption::VALUE_REQUIRED, 'Only remove keys matching the prefix');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$cache = $this->cacheFactory->createDistributed();
$prefix = $input->getOption('prefix');
if ($cache->clear($prefix)) {
if ($prefix) {
$output->writeln('<info>Distributed cache matching prefix ' . $prefix . ' cleared</info>');
} else {
$output->writeln('<info>Distributed cache cleared</info>');
}
return 0;
} else {
$output->writeln('<error>Failed to clear cache</error>');
return 1;
}
}
}

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Memcache;
use OC\Core\Command\Base;
use OCP\ICacheFactory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DistributedDelete extends Base {
public function __construct(
protected ICacheFactory $cacheFactory,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('memcache:distributed:delete')
->setDescription('Delete a value in the distributed memcache')
->addArgument('key', InputArgument::REQUIRED, 'The key to delete');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$cache = $this->cacheFactory->createDistributed();
$key = $input->getArgument('key');
if ($cache->remove($key)) {
$output->writeln('<info>Distributed cache key <info>' . $key . '</info> deleted</info>');
return 0;
} else {
$output->writeln('<error>Failed to delete cache key ' . $key . '</error>');
return 1;
}
}
}

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Memcache;
use OC\Core\Command\Base;
use OCP\ICacheFactory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DistributedGet extends Base {
public function __construct(
protected ICacheFactory $cacheFactory,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('memcache:distributed:get')
->setDescription('Get a value from the distributed memcache')
->addArgument('key', InputArgument::REQUIRED, 'The key to retrieve');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$cache = $this->cacheFactory->createDistributed();
$key = $input->getArgument('key');
$value = $cache->get($key);
$this->writeMixedInOutputFormat($input, $output, $value);
return 0;
}
}

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Memcache;
use OC\Core\Command\Base;
use OC\Core\Command\Config\System\CastHelper;
use OCP\ICacheFactory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DistributedSet extends Base {
public function __construct(
protected ICacheFactory $cacheFactory,
private CastHelper $castHelper,
) {
parent::__construct();
}
protected function configure(): void {
$this
->setName('memcache:distributed:set')
->setDescription('Set a value in the distributed memcache')
->addArgument('key', InputArgument::REQUIRED, 'The key to set')
->addArgument('value', InputArgument::REQUIRED, 'The value to set')
->addOption(
'type',
null,
InputOption::VALUE_REQUIRED,
'Value type [string, integer, float, boolean, json, null]',
'string'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$cache = $this->cacheFactory->createDistributed();
$key = $input->getArgument('key');
$value = $input->getArgument('value');
$type = $input->getOption('type');
['value' => $value, 'readable-value' => $readable] = $this->castHelper->castValue($value, $type);
if ($cache->set($key, $value)) {
$output->writeln('Distributed cache key <info>' . $key . '</info> set to <info>' . $readable . '</info>');
return 0;
} else {
$output->writeln('<error>Failed to set cache key ' . $key . '</error>');
return 1;
}
}
}

@ -158,6 +158,10 @@ if ($config->getSystemValueBool('installed', false)) {
$application->add(Server::get(Command\TaskProcessing\Statistics::class));
$application->add(Server::get(Command\Memcache\RedisCommand::class));
$application->add(Server::get(Command\Memcache\DistributedClear::class));
$application->add(Server::get(Command\Memcache\DistributedDelete::class));
$application->add(Server::get(Command\Memcache\DistributedGet::class));
$application->add(Server::get(Command\Memcache\DistributedSet::class));
} else {
$application->add(Server::get(Command\Maintenance\Install::class));
}

@ -1225,6 +1225,7 @@ return array(
'OC\\Core\\Command\\Config\\Import' => $baseDir . '/core/Command/Config/Import.php',
'OC\\Core\\Command\\Config\\ListConfigs' => $baseDir . '/core/Command/Config/ListConfigs.php',
'OC\\Core\\Command\\Config\\System\\Base' => $baseDir . '/core/Command/Config/System/Base.php',
'OC\\Core\\Command\\Config\\System\\CastHelper' => $baseDir . '/core/Command/Config/System/CastHelper.php',
'OC\\Core\\Command\\Config\\System\\DeleteConfig' => $baseDir . '/core/Command/Config/System/DeleteConfig.php',
'OC\\Core\\Command\\Config\\System\\GetConfig' => $baseDir . '/core/Command/Config/System/GetConfig.php',
'OC\\Core\\Command\\Config\\System\\SetConfig' => $baseDir . '/core/Command/Config/System/SetConfig.php',
@ -1283,6 +1284,10 @@ return array(
'OC\\Core\\Command\\Maintenance\\RepairShareOwnership' => $baseDir . '/core/Command/Maintenance/RepairShareOwnership.php',
'OC\\Core\\Command\\Maintenance\\UpdateHtaccess' => $baseDir . '/core/Command/Maintenance/UpdateHtaccess.php',
'OC\\Core\\Command\\Maintenance\\UpdateTheme' => $baseDir . '/core/Command/Maintenance/UpdateTheme.php',
'OC\\Core\\Command\\Memcache\\DistributedClear' => $baseDir . '/core/Command/Memcache/DistributedClear.php',
'OC\\Core\\Command\\Memcache\\DistributedDelete' => $baseDir . '/core/Command/Memcache/DistributedDelete.php',
'OC\\Core\\Command\\Memcache\\DistributedGet' => $baseDir . '/core/Command/Memcache/DistributedGet.php',
'OC\\Core\\Command\\Memcache\\DistributedSet' => $baseDir . '/core/Command/Memcache/DistributedSet.php',
'OC\\Core\\Command\\Memcache\\RedisCommand' => $baseDir . '/core/Command/Memcache/RedisCommand.php',
'OC\\Core\\Command\\Preview\\Cleanup' => $baseDir . '/core/Command/Preview/Cleanup.php',
'OC\\Core\\Command\\Preview\\Generate' => $baseDir . '/core/Command/Preview/Generate.php',

@ -1274,6 +1274,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Command\\Config\\Import' => __DIR__ . '/../../..' . '/core/Command/Config/Import.php',
'OC\\Core\\Command\\Config\\ListConfigs' => __DIR__ . '/../../..' . '/core/Command/Config/ListConfigs.php',
'OC\\Core\\Command\\Config\\System\\Base' => __DIR__ . '/../../..' . '/core/Command/Config/System/Base.php',
'OC\\Core\\Command\\Config\\System\\CastHelper' => __DIR__ . '/../../..' . '/core/Command/Config/System/CastHelper.php',
'OC\\Core\\Command\\Config\\System\\DeleteConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/DeleteConfig.php',
'OC\\Core\\Command\\Config\\System\\GetConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/GetConfig.php',
'OC\\Core\\Command\\Config\\System\\SetConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/SetConfig.php',
@ -1332,6 +1333,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Command\\Maintenance\\RepairShareOwnership' => __DIR__ . '/../../..' . '/core/Command/Maintenance/RepairShareOwnership.php',
'OC\\Core\\Command\\Maintenance\\UpdateHtaccess' => __DIR__ . '/../../..' . '/core/Command/Maintenance/UpdateHtaccess.php',
'OC\\Core\\Command\\Maintenance\\UpdateTheme' => __DIR__ . '/../../..' . '/core/Command/Maintenance/UpdateTheme.php',
'OC\\Core\\Command\\Memcache\\DistributedClear' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedClear.php',
'OC\\Core\\Command\\Memcache\\DistributedDelete' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedDelete.php',
'OC\\Core\\Command\\Memcache\\DistributedGet' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedGet.php',
'OC\\Core\\Command\\Memcache\\DistributedSet' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedSet.php',
'OC\\Core\\Command\\Memcache\\RedisCommand' => __DIR__ . '/../../..' . '/core/Command/Memcache/RedisCommand.php',
'OC\\Core\\Command\\Preview\\Cleanup' => __DIR__ . '/../../..' . '/core/Command/Preview/Cleanup.php',
'OC\\Core\\Command\\Preview\\Generate' => __DIR__ . '/../../..' . '/core/Command/Preview/Generate.php',

@ -0,0 +1,69 @@
<?php
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace Core\Command\Config\System;
use OC\Core\Command\Config\System\CastHelper;
use Test\TestCase;
class CastHelperTest extends TestCase {
private CastHelper $castHelper;
protected function setUp(): void {
parent::setUp();
$this->castHelper = new CastHelper();
}
public static function castValueProvider(): array {
return [
[null, 'string', ['value' => '', 'readable-value' => 'empty string']],
['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']],
['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']],
['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']],
['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']],
['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']],
['', 'null', ['value' => null, 'readable-value' => 'null']],
['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']],
['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']],
];
}
/**
* @dataProvider castValueProvider
*/
public function testCastValue($value, $type, $expectedValue): void {
$this->assertSame(
$expectedValue,
$this->castHelper->castValue($value, $type)
);
}
public static function castValueInvalidProvider(): array {
return [
['123', 'foobar'],
[null, 'integer'],
['abc', 'integer'],
['76ggg', 'double'],
['true', 'float'],
['foobar', 'boolean'],
];
}
/**
* @dataProvider castValueInvalidProvider
*/
public function testCastValueInvalid($value, $type): void {
$this->expectException(\InvalidArgumentException::class);
$this->castHelper->castValue($value, $type);
}
}

@ -1,4 +1,5 @@
<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@ -7,6 +8,7 @@
namespace Tests\Core\Command\Config\System;
use OC\Core\Command\Config\System\CastHelper;
use OC\Core\Command\Config\System\SetConfig;
use OC\SystemConfig;
use Symfony\Component\Console\Input\InputInterface;
@ -35,7 +37,7 @@ class SetConfigTest extends TestCase {
$this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
/** @var \OC\SystemConfig $systemConfig */
$this->command = new SetConfig($systemConfig);
$this->command = new SetConfig($systemConfig, new CastHelper());
}
@ -55,7 +57,7 @@ class SetConfigTest extends TestCase {
* @param mixed $existingData
* @param mixed $expectedValue
*/
public function testSet($configNames, $newValue, $existingData, $expectedValue): void {
public function testSet($configNames, $newValue, $existingData, $expectedValue) {
$this->systemConfig->expects($this->once())
->method('setValue')
->with($configNames[0], $expectedValue);
@ -88,7 +90,7 @@ class SetConfigTest extends TestCase {
/**
* @dataProvider setUpdateOnlyProvider
*/
public function testSetUpdateOnly($configNames, $existingData): void {
public function testSetUpdateOnly($configNames, $existingData) {
$this->expectException(\UnexpectedValueException::class);
$this->systemConfig->expects($this->never())
@ -135,7 +137,7 @@ class SetConfigTest extends TestCase {
/**
* @dataProvider castValueProvider
*/
public function testCastValue($value, $type, $expectedValue): void {
public function testCastValue($value, $type, $expectedValue) {
$this->assertSame($expectedValue,
$this->invokePrivate($this->command, 'castValue', [$value, $type])
);
@ -156,7 +158,7 @@ class SetConfigTest extends TestCase {
/**
* @dataProvider castValueInvalidProvider
*/
public function testCastValueInvalid($value, $type): void {
public function testCastValueInvalid($value, $type) {
$this->expectException(\InvalidArgumentException::class);
$this->invokePrivate($this->command, 'castValue', [$value, $type]);