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

Commit

Permalink
Merge pull request #49 from LeaseWeb/dev
Browse files Browse the repository at this point in the history
migrate from php5-memcached to php5-memcache
  • Loading branch information
Maurits van der Schee committed Jun 30, 2015
2 parents e0b3fe1 + b09c08e commit 0daee6e
Show file tree
Hide file tree
Showing 27 changed files with 752 additions and 4,556 deletions.
10 changes: 4 additions & 6 deletions Cache/AntiDogPileMemcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Lsw\MemcacheBundle\Cache;

/**
* Class to encapsulate PHP Memcached object to avoid the "Dog Pile" effect
* Class to encapsulate PHP Memcache object to avoid the "Dog Pile" effect
*/
class AntiDogPileMemcache extends LoggingMemcache
{
Expand All @@ -21,8 +21,7 @@ class AntiDogPileMemcache extends LoggingMemcache
*/
public function getAdp($key)
{
$cas=0;
$value = $this->get($key, null, $cas);
$value = $this->get($key, $flags, $cas);
if ($value===false) {
return false;
}
Expand All @@ -32,8 +31,7 @@ public function getAdp($key)
$time = time();
if ($time>$exp) {
$value = implode('|', array($time+$ttl, $ttl, json_encode($val)));
$result = $this->cas($cas, $key, $value, 0);

$result = $this->cas($key, $value, $flags, 0, $cas);
if ($result) {
return false;
}
Expand All @@ -59,7 +57,7 @@ public function setAdp($key, $value, $ttl=0)
}
$time = time();
$value = implode('|', array($time+$ttl, $ttl, json_encode($value)));
$result = $this->set($key, $value, 0);
$result = $this->set($key, $value);

return $result;
}
Expand Down
2,649 changes: 263 additions & 2,386 deletions Cache/LoggingMemcache.php

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cache/LoggingMemcacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
interface LoggingMemcacheInterface
{
/**
* Get the logged calls for this Memcached object
* Get the logged calls for this Memcache object
*
* @return array Array of calls made to the Memcached object
* @return array Array of calls made to the Memcache object
*/
public function getLoggedCalls();

Expand Down
270 changes: 23 additions & 247 deletions Cache/MemcacheInterface.php

Large diffs are not rendered by default.

70 changes: 15 additions & 55 deletions Command/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ protected function configure()
->setName('memcache:clear')
->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');
new InputArgument('pool', InputArgument::REQUIRED, 'The pool'),
));
}

/**
Expand All @@ -47,34 +45,19 @@ 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.'(.*)';
}
$pool = $input->getArgument('pool');

try {
$this->memcache = $this->getContainer()->get('memcache.'.$client);
$this->memcache = $this->getContainer()->get('memcache.'.$pool);

// 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>');
}
$output->writeln($this->memcache->flush()?'<info>OK</info>':'<error>ERROR</error>');
} catch (ServiceNotFoundException $e) {
$output->writeln("<error>client '$client' is not found</error>");
$output->writeln("<error>pool '$pool' is not found</error>");
}
}

/**
* Choose the client
* Choose the pool
*
* @param InputInterface $input Input interface
* @param OutputInterface $output Output interface
Expand All @@ -84,44 +67,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('client')) {
$client = $this->getHelper('dialog')->askAndValidate(
if (!$input->getArgument('pool')) {
$pool = $this->getHelper('dialog')->askAndValidate(
$output,
'Please give the client:',
function($client)
'Please give the pool:',
function($pool)
{
if (empty($client)) {
throw new \Exception('client can not be empty');
if (empty($pool)) {
throw new \Exception('pool can not be empty');
}

return $client;
return $pool;
}
);
$input->setArgument('client', $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;
$input->setArgument('pool', $pool);
}

// 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: 0 additions & 141 deletions Command/ItemsCommand.php

This file was deleted.

28 changes: 14 additions & 14 deletions Command/StatisticsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
* Provides a command-line interface for viewing cache client stats
* Provides a command-line interface for viewing cache pool stats
* Based on Beryllium\CacheBundle by Kevin Boyd <[email protected]>
*/
class StatisticsCommand extends ContainerAwareCommand
Expand All @@ -27,7 +27,7 @@ protected function configure()
->setName('memcache:statistics')
->setDescription('Display Memcache statistics')
->setDefinition(array(
new InputArgument('client', InputArgument::REQUIRED, 'The client'),
new InputArgument('pool', InputArgument::REQUIRED, 'The pool'),
));
}

Expand All @@ -41,17 +41,17 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = $input->getArgument('client');
$pool = $input->getArgument('pool');
try {
$memcache = $this->getContainer()->get('memcache.'.$client);
$memcache = $this->getContainer()->get('memcache.'.$pool);
$output->writeln($this->formatStats($memcache->getStats()));
} catch (ServiceNotFoundException $e) {
$output->writeln("<error>client '$client' is not found</error>");
$output->writeln("<error>pool '$pool' is not found</error>");
}
}

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

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

Expand Down
Loading

0 comments on commit 0daee6e

Please sign in to comment.