From b56bf963c136b8ec8abfb1324a12bf8db5838374 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 15 May 2025 17:52:31 +0200 Subject: [PATCH] feat: add commands to get and set memcache values Signed-off-by: Robin Appelman --- core/Command/Memcache/DistributedGet.php | 40 +++++++++++++++++ core/Command/Memcache/DistributedSet.php | 57 ++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 core/Command/Memcache/DistributedGet.php create mode 100644 core/Command/Memcache/DistributedSet.php diff --git a/core/Command/Memcache/DistributedGet.php b/core/Command/Memcache/DistributedGet.php new file mode 100644 index 00000000000..bf1b00d312d --- /dev/null +++ b/core/Command/Memcache/DistributedGet.php @@ -0,0 +1,40 @@ +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; + } +} diff --git a/core/Command/Memcache/DistributedSet.php b/core/Command/Memcache/DistributedSet.php new file mode 100644 index 00000000000..0f31c22f730 --- /dev/null +++ b/core/Command/Memcache/DistributedSet.php @@ -0,0 +1,57 @@ +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 ' . $key . ' set to ' . $readable . ''); + return 0; + } else { + $output->writeln('Failed to set cache key ' . $key . ''); + return 1; + } + } +}