Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:LeaseWeb/LswMemcacheBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed Oct 2, 2014
2 parents a8b154c + a7d0024 commit 84a49fe
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 3 deletions.
53 changes: 50 additions & 3 deletions Command/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lsw\MemcacheBundle\Command;

use Lsw\MemcacheBundle\Cache\AntiDogPileMemcache;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -14,6 +15,10 @@
*/
class ClearCommand extends ContainerAwareCommand
{
/**
* @var AntiDogPileMemcache
*/
private $memcache;

/**
* Configure the CLI task
Expand All @@ -27,7 +32,9 @@ protected function configure()
->setDescription('Invalidate all Memcache items')
->setDefinition(array(
new InputArgument('client', InputArgument::REQUIRED, 'The client'),
));
))
->addOption('prefix', null, InputOption::VALUE_REQUIRED, 'Only clears items with specific prefix')
->addOption('regex', null, InputOption::VALUE_REQUIRED, 'Only clears items matching the expression');
}

/**
Expand All @@ -41,9 +48,26 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = $input->getArgument('client');
$prefix = $input->getOption('prefix');
$regex = $input->getOption('regex');

if(!empty($prefix) && !empty($regex)) {
throw new \InvalidArgumentException('you cannot filter by prefix and regex');
}

if(!empty($prefix)) {
$regex = '^'.$prefix.'(.*)';
}

try {
$memcache = $this->getContainer()->get('memcache.'.$client);
$output->writeln($memcache->flush()?'<info>OK</info>':'<error>ERROR</error>');
$this->memcache = $this->getContainer()->get('memcache.'.$client);

// flush/delete keys
if(empty($regex)) {
$output->writeln($this->memcache->flush()?'<info>OK</info>':'<error>ERROR</error>');
} else {
$output->writeln($this->deleteByRegex($regex)?'<info>OK</info>':'<error>ERROR</error>');
}
} catch (ServiceNotFoundException $e) {
$output->writeln("<error>client '$client' is not found</error>");
}
Expand Down Expand Up @@ -77,4 +101,27 @@ function($client)
}
}

/**
* delete keys by regular expression
*
* @param $regex
* @return bool|void
*/
private function deleteByRegex($regex)
{
// load keys
$keys = $this->memcache->getAllKeys();

if(!$keys) {
return false;
}

// filter keys
$deleteKeys = preg_grep('@'.$regex.'@', $keys);
// reset prefix
$this->memcache->setOption(\Memcached::OPT_PREFIX_KEY, '');
// delete keys
return $this->memcache->deleteMulti($deleteKeys);
}

}
141 changes: 141 additions & 0 deletions Command/ItemsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Lsw\MemcacheBundle\Command;

use Lsw\MemcacheBundle\Cache\AntiDogPileMemcache;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
* Provides a command-line interface for dumping memcache keys
*/
class ItemsCommand extends ContainerAwareCommand
{
/**
* @var AntiDogPileMemcache
*/
private $memcache;

/**
* Configure the CLI task
*
* @return void
*/
protected function configure()
{
$this
->setName('memcache:items')
->setDescription('Lists Memcache items')
->setDefinition(array(
new InputArgument('client', InputArgument::REQUIRED, 'The client'),
))
->addOption('prefix', null, InputOption::VALUE_REQUIRED, 'Only lists items with specific prefix')
->addOption('regex', null, InputOption::VALUE_REQUIRED, 'Only lists items matching the expression');
}

/**
* Execute the CLI task
*
* @param InputInterface $input Command input
* @param OutputInterface $output Command output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = $input->getArgument('client');
$prefix = $input->getOption('prefix');
$regex = $input->getOption('regex');

if(!empty($prefix) && !empty($regex)) {
throw new \InvalidArgumentException('you cannot filter by prefix and regex');
}

if(!empty($prefix)) {
$regex = '^'.$prefix.'(.*)';
}

try {
$this->memcache = $this->getContainer()->get('memcache.'.$client);
// reset prefix
$this->memcache->setOption(\Memcached::OPT_PREFIX_KEY, '');

// load keys
if(empty($regex)) {
$keys = $this->getAllKeys();
} else {
$keys = $this->getKeysByRegex($regex);
}

$output->writeln("<info>".count($keys)." items found</info>");

foreach ($keys as $key) {
// check if key has valid data
if($this->memcache->get($key)) {
$state = '<info>valid</info>';
} else {
$state = '<comment>invalid</comment>';
}
$output->writeln($key.' ['.$state.']');
}

} catch (ServiceNotFoundException $e) {
$output->writeln("<error>client '$client' is not found</error>");
}
}

/**
* Choose the client
*
* @param InputInterface $input Input interface
* @param OutputInterface $output Output interface
*
* @see Command
* @return mixed
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('client')) {
$client = $this->getHelper('dialog')->askAndValidate(
$output,
'Please give the client:',
function($client)
{
if (empty($client)) {
throw new \Exception('client can not be empty');
}

return $client;
}
);
$input->setArgument('client', $client);
}
}

/**
* load all keys
*
* @return array
*/
private function getAllKeys()
{
$keys = $this->memcache->getAllKeys();
return $keys ? $keys : array();
}

/**
* load keys filtered by regex
*
* @return array
*/
private function getKeysByRegex($regex)
{
$keys = $this->getAllKeys();
return preg_grep('@'.$regex.'@', $keys);
}

}

0 comments on commit 84a49fe

Please sign in to comment.