From b5737361c55b56b8d830ce12c7debb11aedc530c Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 21 Sep 2021 15:15:37 +0300 Subject: [PATCH] feat(console): add CacheSetMultipleCommand #543 --- .../Cache/CacheSetMultipleCommand.php | 43 +++++++++++++++++++ .../Console/FlextypeConsoleApplication.php | 2 + 2 files changed, 45 insertions(+) create mode 100644 src/flextype/core/Console/Commands/Cache/CacheSetMultipleCommand.php diff --git a/src/flextype/core/Console/Commands/Cache/CacheSetMultipleCommand.php b/src/flextype/core/Console/Commands/Cache/CacheSetMultipleCommand.php new file mode 100644 index 00000000..db01b985 --- /dev/null +++ b/src/flextype/core/Console/Commands/Cache/CacheSetMultipleCommand.php @@ -0,0 +1,43 @@ +setName('cache:set-multiple'); + $this->setDescription('Set multiple items.'); + $this->addArgument('values', InputArgument::REQUIRED, 'Values.'); + $this->addArgument('ttl', InputArgument::OPTIONAL, 'Time To Live.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $values = $input->getArgument('values') ? serializers()->json()->decode($input->getArgument('values')) : []; + $ttl = $input->getArgument('ttl') ?? 300; + + if (cache()->setMultiple($values, $ttl)) { + $io->success('Cache items with keys ' . implode(', ', array_keys($values)) . ' create.'); + return Command::SUCCESS; + } else { + $io->error('Cache items with keys ' . implode(', ', array_keys($values)) . ' wasn\'t created.'); + return Command::FAILURE; + } + } +} \ No newline at end of file diff --git a/src/flextype/core/Console/FlextypeConsoleApplication.php b/src/flextype/core/Console/FlextypeConsoleApplication.php index d0839e3f..0391ab06 100644 --- a/src/flextype/core/Console/FlextypeConsoleApplication.php +++ b/src/flextype/core/Console/FlextypeConsoleApplication.php @@ -21,6 +21,7 @@ use Flextype\Console\Commands\Cache\CacheDeleteCommand; use Flextype\Console\Commands\Cache\CacheSetCommand; use Flextype\Console\Commands\Cache\CacheGetCommand; use Flextype\Console\Commands\Cache\CacheGetMultipleCommand; +use Flextype\Console\Commands\Cache\CacheSetMultipleCommand; use Flextype\Console\Commands\Cache\CacheClearCommand; use Flextype\Console\Commands\Cache\CacheHasCommand; use Symfony\Component\Console\Input\InputInterface; @@ -41,6 +42,7 @@ class FlextypeConsoleApplication extends ConsoleApplication console()->add(new CacheSetCommand()); console()->add(new CacheGetCommand()); console()->add(new CacheGetMultipleCommand()); + console()->add(new CacheSetMultipleCommand()); console()->add(new CacheDeleteCommand()); console()->add(new CacheClearCommand()); console()->add(new CacheHasCommand());