Skip to content

Commit 5c28140

Browse files
authored
Merge pull request #54641 from nextcloud/backport/52880/stable31
[stable31] Add commands to set/get/delete/clear the distributed memcache
2 parents 461694e + 8c72051 commit 5c28140

File tree

11 files changed

+355
-6
lines changed

11 files changed

+355
-6
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OC\Core\Command\Config\System;
10+
11+
class CastHelper {
12+
/**
13+
* @return array{value: mixed, readable-value: string}
14+
*/
15+
public function castValue(?string $value, string $type): array {
16+
switch ($type) {
17+
case 'integer':
18+
case 'int':
19+
if (!is_numeric($value)) {
20+
throw new \InvalidArgumentException('Non-numeric value specified');
21+
}
22+
return [
23+
'value' => (int)$value,
24+
'readable-value' => 'integer ' . (int)$value,
25+
];
26+
27+
case 'double':
28+
case 'float':
29+
if (!is_numeric($value)) {
30+
throw new \InvalidArgumentException('Non-numeric value specified');
31+
}
32+
return [
33+
'value' => (float)$value,
34+
'readable-value' => 'double ' . (float)$value,
35+
];
36+
37+
case 'boolean':
38+
case 'bool':
39+
$value = strtolower($value);
40+
return match ($value) {
41+
'true' => [
42+
'value' => true,
43+
'readable-value' => 'boolean ' . $value,
44+
],
45+
'false' => [
46+
'value' => false,
47+
'readable-value' => 'boolean ' . $value,
48+
],
49+
default => throw new \InvalidArgumentException('Unable to parse value as boolean'),
50+
};
51+
52+
case 'null':
53+
return [
54+
'value' => null,
55+
'readable-value' => 'null',
56+
];
57+
58+
case 'string':
59+
$value = (string)$value;
60+
return [
61+
'value' => $value,
62+
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
63+
];
64+
65+
case 'json':
66+
$value = json_decode($value, true);
67+
return [
68+
'value' => $value,
69+
'readable-value' => 'json ' . json_encode($value),
70+
];
71+
72+
default:
73+
throw new \InvalidArgumentException('Invalid type');
74+
}
75+
}
76+
}

core/Command/Config/System/SetConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class SetConfig extends Base {
1818
public function __construct(
1919
SystemConfig $systemConfig,
20+
private CastHelper $castHelper,
2021
) {
2122
parent::__construct($systemConfig);
2223
}
@@ -57,7 +58,7 @@ protected function configure() {
5758
protected function execute(InputInterface $input, OutputInterface $output): int {
5859
$configNames = $input->getArgument('name');
5960
$configName = $configNames[0];
60-
$configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
61+
$configValue = $this->castHelper->castValue($input->getOption('value'), $input->getOption('type'));
6162
$updateOnly = $input->getOption('update-only');
6263

6364
if (count($configNames) > 1) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OC\Core\Command\Memcache;
10+
11+
use OC\Core\Command\Base;
12+
use OCP\ICacheFactory;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Input\InputOption;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
17+
class DistributedClear extends Base {
18+
public function __construct(
19+
protected ICacheFactory $cacheFactory,
20+
) {
21+
parent::__construct();
22+
}
23+
24+
protected function configure(): void {
25+
$this
26+
->setName('memcache:distributed:clear')
27+
->setDescription('Clear values from the distributed memcache')
28+
->addOption('prefix', null, InputOption::VALUE_REQUIRED, 'Only remove keys matching the prefix');
29+
parent::configure();
30+
}
31+
32+
protected function execute(InputInterface $input, OutputInterface $output): int {
33+
$cache = $this->cacheFactory->createDistributed();
34+
$prefix = $input->getOption('prefix');
35+
if ($cache->clear($prefix)) {
36+
if ($prefix) {
37+
$output->writeln('<info>Distributed cache matching prefix ' . $prefix . ' cleared</info>');
38+
} else {
39+
$output->writeln('<info>Distributed cache cleared</info>');
40+
}
41+
return 0;
42+
} else {
43+
$output->writeln('<error>Failed to clear cache</error>');
44+
return 1;
45+
}
46+
}
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OC\Core\Command\Memcache;
10+
11+
use OC\Core\Command\Base;
12+
use OCP\ICacheFactory;
13+
use Symfony\Component\Console\Input\InputArgument;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
17+
class DistributedDelete extends Base {
18+
public function __construct(
19+
protected ICacheFactory $cacheFactory,
20+
) {
21+
parent::__construct();
22+
}
23+
24+
protected function configure(): void {
25+
$this
26+
->setName('memcache:distributed:delete')
27+
->setDescription('Delete a value in the distributed memcache')
28+
->addArgument('key', InputArgument::REQUIRED, 'The key to delete');
29+
parent::configure();
30+
}
31+
32+
protected function execute(InputInterface $input, OutputInterface $output): int {
33+
$cache = $this->cacheFactory->createDistributed();
34+
$key = $input->getArgument('key');
35+
if ($cache->remove($key)) {
36+
$output->writeln('<info>Distributed cache key <info>' . $key . '</info> deleted</info>');
37+
return 0;
38+
} else {
39+
$output->writeln('<error>Failed to delete cache key ' . $key . '</error>');
40+
return 1;
41+
}
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OC\Core\Command\Memcache;
10+
11+
use OC\Core\Command\Base;
12+
use OCP\ICacheFactory;
13+
use Symfony\Component\Console\Input\InputArgument;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
17+
class DistributedGet extends Base {
18+
public function __construct(
19+
protected ICacheFactory $cacheFactory,
20+
) {
21+
parent::__construct();
22+
}
23+
24+
protected function configure(): void {
25+
$this
26+
->setName('memcache:distributed:get')
27+
->setDescription('Get a value from the distributed memcache')
28+
->addArgument('key', InputArgument::REQUIRED, 'The key to retrieve');
29+
parent::configure();
30+
}
31+
32+
protected function execute(InputInterface $input, OutputInterface $output): int {
33+
$cache = $this->cacheFactory->createDistributed();
34+
$key = $input->getArgument('key');
35+
36+
$value = $cache->get($key);
37+
$this->writeMixedInOutputFormat($input, $output, $value);
38+
return 0;
39+
}
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OC\Core\Command\Memcache;
10+
11+
use OC\Core\Command\Base;
12+
use OC\Core\Command\Config\System\CastHelper;
13+
use OCP\ICacheFactory;
14+
use Symfony\Component\Console\Input\InputArgument;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputOption;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
19+
class DistributedSet extends Base {
20+
public function __construct(
21+
protected ICacheFactory $cacheFactory,
22+
private CastHelper $castHelper,
23+
) {
24+
parent::__construct();
25+
}
26+
27+
protected function configure(): void {
28+
$this
29+
->setName('memcache:distributed:set')
30+
->setDescription('Set a value in the distributed memcache')
31+
->addArgument('key', InputArgument::REQUIRED, 'The key to set')
32+
->addArgument('value', InputArgument::REQUIRED, 'The value to set')
33+
->addOption(
34+
'type',
35+
null,
36+
InputOption::VALUE_REQUIRED,
37+
'Value type [string, integer, float, boolean, json, null]',
38+
'string'
39+
);
40+
parent::configure();
41+
}
42+
43+
protected function execute(InputInterface $input, OutputInterface $output): int {
44+
$cache = $this->cacheFactory->createDistributed();
45+
$key = $input->getArgument('key');
46+
$value = $input->getArgument('value');
47+
$type = $input->getOption('type');
48+
['value' => $value, 'readable-value' => $readable] = $this->castHelper->castValue($value, $type);
49+
if ($cache->set($key, $value)) {
50+
$output->writeln('Distributed cache key <info>' . $key . '</info> set to <info>' . $readable . '</info>');
51+
return 0;
52+
} else {
53+
$output->writeln('<error>Failed to set cache key ' . $key . '</error>');
54+
return 1;
55+
}
56+
}
57+
}

core/register_command.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@
158158
$application->add(Server::get(Command\TaskProcessing\Statistics::class));
159159

160160
$application->add(Server::get(Command\Memcache\RedisCommand::class));
161+
$application->add(Server::get(Command\Memcache\DistributedClear::class));
162+
$application->add(Server::get(Command\Memcache\DistributedDelete::class));
163+
$application->add(Server::get(Command\Memcache\DistributedGet::class));
164+
$application->add(Server::get(Command\Memcache\DistributedSet::class));
161165
} else {
162166
$application->add(Server::get(Command\Maintenance\Install::class));
163167
}

lib/composer/composer/autoload_classmap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,7 @@
12251225
'OC\\Core\\Command\\Config\\Import' => $baseDir . '/core/Command/Config/Import.php',
12261226
'OC\\Core\\Command\\Config\\ListConfigs' => $baseDir . '/core/Command/Config/ListConfigs.php',
12271227
'OC\\Core\\Command\\Config\\System\\Base' => $baseDir . '/core/Command/Config/System/Base.php',
1228+
'OC\\Core\\Command\\Config\\System\\CastHelper' => $baseDir . '/core/Command/Config/System/CastHelper.php',
12281229
'OC\\Core\\Command\\Config\\System\\DeleteConfig' => $baseDir . '/core/Command/Config/System/DeleteConfig.php',
12291230
'OC\\Core\\Command\\Config\\System\\GetConfig' => $baseDir . '/core/Command/Config/System/GetConfig.php',
12301231
'OC\\Core\\Command\\Config\\System\\SetConfig' => $baseDir . '/core/Command/Config/System/SetConfig.php',
@@ -1283,6 +1284,10 @@
12831284
'OC\\Core\\Command\\Maintenance\\RepairShareOwnership' => $baseDir . '/core/Command/Maintenance/RepairShareOwnership.php',
12841285
'OC\\Core\\Command\\Maintenance\\UpdateHtaccess' => $baseDir . '/core/Command/Maintenance/UpdateHtaccess.php',
12851286
'OC\\Core\\Command\\Maintenance\\UpdateTheme' => $baseDir . '/core/Command/Maintenance/UpdateTheme.php',
1287+
'OC\\Core\\Command\\Memcache\\DistributedClear' => $baseDir . '/core/Command/Memcache/DistributedClear.php',
1288+
'OC\\Core\\Command\\Memcache\\DistributedDelete' => $baseDir . '/core/Command/Memcache/DistributedDelete.php',
1289+
'OC\\Core\\Command\\Memcache\\DistributedGet' => $baseDir . '/core/Command/Memcache/DistributedGet.php',
1290+
'OC\\Core\\Command\\Memcache\\DistributedSet' => $baseDir . '/core/Command/Memcache/DistributedSet.php',
12861291
'OC\\Core\\Command\\Memcache\\RedisCommand' => $baseDir . '/core/Command/Memcache/RedisCommand.php',
12871292
'OC\\Core\\Command\\Preview\\Cleanup' => $baseDir . '/core/Command/Preview/Cleanup.php',
12881293
'OC\\Core\\Command\\Preview\\Generate' => $baseDir . '/core/Command/Preview/Generate.php',

lib/composer/composer/autoload_static.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
12741274
'OC\\Core\\Command\\Config\\Import' => __DIR__ . '/../../..' . '/core/Command/Config/Import.php',
12751275
'OC\\Core\\Command\\Config\\ListConfigs' => __DIR__ . '/../../..' . '/core/Command/Config/ListConfigs.php',
12761276
'OC\\Core\\Command\\Config\\System\\Base' => __DIR__ . '/../../..' . '/core/Command/Config/System/Base.php',
1277+
'OC\\Core\\Command\\Config\\System\\CastHelper' => __DIR__ . '/../../..' . '/core/Command/Config/System/CastHelper.php',
12771278
'OC\\Core\\Command\\Config\\System\\DeleteConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/DeleteConfig.php',
12781279
'OC\\Core\\Command\\Config\\System\\GetConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/GetConfig.php',
12791280
'OC\\Core\\Command\\Config\\System\\SetConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/SetConfig.php',
@@ -1332,6 +1333,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
13321333
'OC\\Core\\Command\\Maintenance\\RepairShareOwnership' => __DIR__ . '/../../..' . '/core/Command/Maintenance/RepairShareOwnership.php',
13331334
'OC\\Core\\Command\\Maintenance\\UpdateHtaccess' => __DIR__ . '/../../..' . '/core/Command/Maintenance/UpdateHtaccess.php',
13341335
'OC\\Core\\Command\\Maintenance\\UpdateTheme' => __DIR__ . '/../../..' . '/core/Command/Maintenance/UpdateTheme.php',
1336+
'OC\\Core\\Command\\Memcache\\DistributedClear' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedClear.php',
1337+
'OC\\Core\\Command\\Memcache\\DistributedDelete' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedDelete.php',
1338+
'OC\\Core\\Command\\Memcache\\DistributedGet' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedGet.php',
1339+
'OC\\Core\\Command\\Memcache\\DistributedSet' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedSet.php',
13351340
'OC\\Core\\Command\\Memcache\\RedisCommand' => __DIR__ . '/../../..' . '/core/Command/Memcache/RedisCommand.php',
13361341
'OC\\Core\\Command\\Preview\\Cleanup' => __DIR__ . '/../../..' . '/core/Command/Preview/Cleanup.php',
13371342
'OC\\Core\\Command\\Preview\\Generate' => __DIR__ . '/../../..' . '/core/Command/Preview/Generate.php',

0 commit comments

Comments
 (0)