Skip to content

[Cache] Add cache pool get & set command #37880

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
`cache_clearer`, `filesystem` and `validator` services to private.
* Added `TemplateAwareDataCollectorInterface` and `AbstractDataCollector` to simplify custom data collector creation and leverage autoconfiguration
* Add `cache.adapter.redis_tag_aware` tag to use `RedisCacheAwareAdapter`
* Added `cache:pool:get` and `cache:pool:set` commands to respectively get and set cache pools key value

5.1.0
-----
Expand Down
79 changes: 79 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Command/CachePoolGetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Dumper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Gets a cache pool key value.
*
* @author Antoine Makdessi <amakdessi@me.com>
*/
final class CachePoolGetCommand extends Command
{
protected static $defaultName = 'cache:pool:get';

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDescription('Gets a cache pool key value.')
->addArgument('pool', InputArgument::REQUIRED, 'The cache pool from which to get a key')
->addArgument('keys', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The cache key(s) to get a value on')
->setHelp(<<<'EOF'
The <info>%command.name%</info> gets a value from given cache pool key(s).

%command.full_name% <pool> <keys>
EOF
)
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$container = $this->getApplication()->getKernel()->getContainer();

$pool = $input->getArgument('pool');
if (!$container->has($pool)) {
$io->error('Error loading the cache pool.');

return 1;
}

$cache = $container->get($pool);
$dump = new Dumper($output);

$keys = $input->getArgument('keys');
foreach ($keys as $key) {
$io->section(sprintf('"%s" "%s"', $pool, $key));

if ($cache->hasItem($key)) {
$io->writeln($dump($cache->getItem($key)->get()));
} else {
$io->note(sprintf('Cache item key "%s" does not exist in cache pool "%s".', $key, $pool));
}
}

return 0;
}
}
82 changes: 82 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Command/CachePoolSetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Sets a cache pool key value.
*
* @author Antoine Makdessi <amakdessi@me.com>
*/
final class CachePoolSetCommand extends Command
{
protected static $defaultName = 'cache:pool:set';

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDescription('Sets a cache pool key value.')
->addArgument('pool', InputArgument::REQUIRED, 'The cache pool to which to set a key')
->addArgument('key', InputArgument::REQUIRED, 'The cache key to set a value on')
->addArgument('value', InputArgument::REQUIRED, 'The value to set in the cache pool key')
->setHelp(<<<'EOF'
The <info>%command.name%</info> sets a value on a key on a given cache pool.

%command.full_name% <pool> <key> <value>
EOF
)
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$container = $this->getApplication()->getKernel()->getContainer();

$pool = $input->getArgument('pool');
if (!$container->has($pool)) {
$io->error('Error loading the cache pool.');

return 1;
}

$cache = $container->get($pool);

$key = $input->getArgument('key');
$value = $input->getArgument('value');

$cacheItem = $cache->getItem($key);
$cacheItem->set($value);
$saved = $cache->save($cacheItem);

if (!$saved) {
$io->error('Error saving the value.');

return 1;
}

$io->success(sprintf('Cache item value was successfully saved in "%s" "%s".', $pool, $key));

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand;
use Symfony\Bundle\FrameworkBundle\Command\CachePoolClearCommand;
use Symfony\Bundle\FrameworkBundle\Command\CachePoolDeleteCommand;
use Symfony\Bundle\FrameworkBundle\Command\CachePoolGetCommand;
use Symfony\Bundle\FrameworkBundle\Command\CachePoolListCommand;
use Symfony\Bundle\FrameworkBundle\Command\CachePoolPruneCommand;
use Symfony\Bundle\FrameworkBundle\Command\CachePoolSetCommand;
use Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand;
use Symfony\Bundle\FrameworkBundle\Command\ConfigDebugCommand;
use Symfony\Bundle\FrameworkBundle\Command\ConfigDumpReferenceCommand;
Expand Down Expand Up @@ -95,6 +97,12 @@
])
->tag('console.command', ['command' => 'cache:pool:delete'])

->set('console.command.cache_pool_get', CachePoolGetCommand::class)
->tag('console.command', ['command' => 'cache:pool:get'])

->set('console.command.cache_pool_set', CachePoolSetCommand::class)
->tag('console.command', ['command' => 'cache:pool:set'])

->set('console.command.cache_pool_list', CachePoolListCommand::class)
->args([
null,
Expand Down