Skip to content

Commit 8689a4a

Browse files
committed
feat: add commands to get and set memcache values
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent d61ec62 commit 8689a4a

5 files changed

Lines changed: 106 additions & 0 deletions

File tree

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
use OC\Core\Command\Maintenance\UpdateHtaccess;
7070
use OC\Core\Command\Maintenance\UpdateTheme;
7171
use OC\Core\Command\Memcache\RedisCommand;
72+
use OC\Core\Command\Memcache\DistributedGet;
73+
use OC\Core\Command\Memcache\DistributedSet;
74+
use OC\Core\Command\Memcache\RedisCommand;
7275
use OC\Core\Command\Preview\Generate;
7376
use OC\Core\Command\Preview\ResetRenderedTexts;
7477
use OC\Core\Command\Security\BruteforceAttempts;
@@ -245,6 +248,8 @@
245248
$application->add(Server::get(Statistics::class));
246249

247250
$application->add(Server::get(RedisCommand::class));
251+
$application->add(Server::get(DistributedGet::class));
252+
$application->add(Server::get(DistributedSet::class));
248253
} else {
249254
$application->add(Server::get(Command\Maintenance\Install::class));
250255
}

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,8 @@
12931293
'OC\\Core\\Command\\Maintenance\\RepairShareOwnership' => $baseDir . '/core/Command/Maintenance/RepairShareOwnership.php',
12941294
'OC\\Core\\Command\\Maintenance\\UpdateHtaccess' => $baseDir . '/core/Command/Maintenance/UpdateHtaccess.php',
12951295
'OC\\Core\\Command\\Maintenance\\UpdateTheme' => $baseDir . '/core/Command/Maintenance/UpdateTheme.php',
1296+
'OC\\Core\\Command\\Memcache\\DistributedGet' => $baseDir . '/core/Command/Memcache/DistributedGet.php',
1297+
'OC\\Core\\Command\\Memcache\\DistributedSet' => $baseDir . '/core/Command/Memcache/DistributedSet.php',
12961298
'OC\\Core\\Command\\Memcache\\RedisCommand' => $baseDir . '/core/Command/Memcache/RedisCommand.php',
12971299
'OC\\Core\\Command\\Preview\\Cleanup' => $baseDir . '/core/Command/Preview/Cleanup.php',
12981300
'OC\\Core\\Command\\Preview\\Generate' => $baseDir . '/core/Command/Preview/Generate.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
13341334
'OC\\Core\\Command\\Maintenance\\RepairShareOwnership' => __DIR__ . '/../../..' . '/core/Command/Maintenance/RepairShareOwnership.php',
13351335
'OC\\Core\\Command\\Maintenance\\UpdateHtaccess' => __DIR__ . '/../../..' . '/core/Command/Maintenance/UpdateHtaccess.php',
13361336
'OC\\Core\\Command\\Maintenance\\UpdateTheme' => __DIR__ . '/../../..' . '/core/Command/Maintenance/UpdateTheme.php',
1337+
'OC\\Core\\Command\\Memcache\\DistributedGet' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedGet.php',
1338+
'OC\\Core\\Command\\Memcache\\DistributedSet' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedSet.php',
13371339
'OC\\Core\\Command\\Memcache\\RedisCommand' => __DIR__ . '/../../..' . '/core/Command/Memcache/RedisCommand.php',
13381340
'OC\\Core\\Command\\Preview\\Cleanup' => __DIR__ . '/../../..' . '/core/Command/Preview/Cleanup.php',
13391341
'OC\\Core\\Command\\Preview\\Generate' => __DIR__ . '/../../..' . '/core/Command/Preview/Generate.php',

0 commit comments

Comments
 (0)