Skip to content

Commit 3fa81d0

Browse files
authored
Merge pull request #52880 from nextcloud/memcache-commands
Add commands to set/get/delete/clear the distributed memcache
2 parents 29ff88d + b5b2eb9 commit 3fa81d0

11 files changed

Lines changed: 354 additions & 125 deletions

File tree

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 & 75 deletions
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) {
@@ -80,80 +81,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8081
return 0;
8182
}
8283

83-
/**
84-
* @param string $value
85-
* @param string $type
86-
* @return mixed
87-
* @throws \InvalidArgumentException
88-
*/
89-
protected function castValue($value, $type) {
90-
switch ($type) {
91-
case 'integer':
92-
case 'int':
93-
if (!is_numeric($value)) {
94-
throw new \InvalidArgumentException('Non-numeric value specified');
95-
}
96-
return [
97-
'value' => (int)$value,
98-
'readable-value' => 'integer ' . (int)$value,
99-
];
100-
101-
case 'double':
102-
case 'float':
103-
if (!is_numeric($value)) {
104-
throw new \InvalidArgumentException('Non-numeric value specified');
105-
}
106-
return [
107-
'value' => (float)$value,
108-
'readable-value' => 'double ' . (float)$value,
109-
];
110-
111-
case 'boolean':
112-
case 'bool':
113-
$value = strtolower($value);
114-
switch ($value) {
115-
case 'true':
116-
return [
117-
'value' => true,
118-
'readable-value' => 'boolean ' . $value,
119-
];
120-
121-
case 'false':
122-
return [
123-
'value' => false,
124-
'readable-value' => 'boolean ' . $value,
125-
];
126-
127-
default:
128-
throw new \InvalidArgumentException('Unable to parse value as boolean');
129-
}
130-
131-
// no break
132-
case 'null':
133-
return [
134-
'value' => null,
135-
'readable-value' => 'null',
136-
];
137-
138-
case 'string':
139-
$value = (string)$value;
140-
return [
141-
'value' => $value,
142-
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
143-
];
144-
145-
case 'json':
146-
$value = json_decode($value, true);
147-
return [
148-
'value' => $value,
149-
'readable-value' => 'json ' . json_encode($value),
150-
];
151-
152-
default:
153-
throw new \InvalidArgumentException('Invalid type');
154-
}
155-
}
156-
15784
/**
15885
* @param array $configNames
15986
* @param mixed $existingValues
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
use OC\Core\Command\Maintenance\RepairShareOwnership;
6969
use OC\Core\Command\Maintenance\UpdateHtaccess;
7070
use OC\Core\Command\Maintenance\UpdateTheme;
71+
use OC\Core\Command\Memcache\DistributedClear;
72+
use OC\Core\Command\Memcache\DistributedDelete;
73+
use OC\Core\Command\Memcache\DistributedGet;
74+
use OC\Core\Command\Memcache\DistributedSet;
7175
use OC\Core\Command\Memcache\RedisCommand;
7276
use OC\Core\Command\Preview\Generate;
7377
use OC\Core\Command\Preview\ResetRenderedTexts;
@@ -249,6 +253,10 @@
249253
$application->add(Server::get(Statistics::class));
250254

251255
$application->add(Server::get(RedisCommand::class));
256+
$application->add(Server::get(DistributedClear::class));
257+
$application->add(Server::get(DistributedDelete::class));
258+
$application->add(Server::get(DistributedGet::class));
259+
$application->add(Server::get(DistributedSet::class));
252260
} else {
253261
$application->add(Server::get(Command\Maintenance\Install::class));
254262
}

0 commit comments

Comments
 (0)