You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.7 KiB

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