-
-
Couldn't load subscription status.
- Fork 4.6k
[stable30] Add commands to set/get/delete/clear the distributed memcache #54642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9cef073
refactor: split off value casting out of config:system:set command
icewind1991 b56bf96
feat: add commands to get and set memcache values
icewind1991 0b6d5dc
feat: add command to delete memcache key
icewind1991 38842ba
feat: add command to clear memcache
icewind1991 186a59b
fix: added set/get/delete/clear to register command
yemkareems d66c29a
fix: composer run cs:fix
yemkareems 738d927
fix: generated autoload files for get/set/clear/delete
yemkareems 67eb432
Merge branch 'stable30' into backport/52880/stable30
yemkareems File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
Check noticeCode scanning / Psalm PossiblyNullArgument Note
Argument 1 of json_decode cannot be null, possibly null value provided
|
||
| return [ | ||
| 'value' => $value, | ||
| 'readable-value' => 'json ' . json_encode($value), | ||
| ]; | ||
|
|
||
| default: | ||
| throw new \InvalidArgumentException('Invalid type'); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Psalm
PossiblyNullArgument Note