|
| 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 | +} |
0 commit comments