Skip to content

Commit b5b2eb9

Browse files
committed
feat: add command to clear memcache
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 131115f commit b5b2eb9

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

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+
}

core/register_command.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
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\RedisCommand;
71+
use OC\Core\Command\Memcache\DistributedClear;
7272
use OC\Core\Command\Memcache\DistributedDelete;
7373
use OC\Core\Command\Memcache\DistributedGet;
7474
use OC\Core\Command\Memcache\DistributedSet;
@@ -249,6 +249,7 @@
249249
$application->add(Server::get(Statistics::class));
250250

251251
$application->add(Server::get(RedisCommand::class));
252+
$application->add(Server::get(DistributedClear::class));
252253
$application->add(Server::get(DistributedDelete::class));
253254
$application->add(Server::get(DistributedGet::class));
254255
$application->add(Server::get(DistributedSet::class));

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,7 @@
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\\DistributedClear' => $baseDir . '/core/Command/Memcache/DistributedClear.php',
12961297
'OC\\Core\\Command\\Memcache\\DistributedDelete' => $baseDir . '/core/Command/Memcache/DistributedDelete.php',
12971298
'OC\\Core\\Command\\Memcache\\DistributedGet' => $baseDir . '/core/Command/Memcache/DistributedGet.php',
12981299
'OC\\Core\\Command\\Memcache\\DistributedSet' => $baseDir . '/core/Command/Memcache/DistributedSet.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,7 @@ 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\\DistributedClear' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedClear.php',
13371338
'OC\\Core\\Command\\Memcache\\DistributedDelete' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedDelete.php',
13381339
'OC\\Core\\Command\\Memcache\\DistributedGet' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedGet.php',
13391340
'OC\\Core\\Command\\Memcache\\DistributedSet' => __DIR__ . '/../../..' . '/core/Command/Memcache/DistributedSet.php',

0 commit comments

Comments
 (0)