Skip to content

Commit

Permalink
Allow multiple key delete support
Browse files Browse the repository at this point in the history
The Redis DEL command allows deletion of multiple keys with a single
command.
Updated del() function to support multiple keys as well. Keys can be
passed as an array of strings or as multiple arguments to the function.
  • Loading branch information
dchancogne committed Feb 14, 2014
1 parent 9d39047 commit 55ea884
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
composer.lock
composer.phar
27 changes: 15 additions & 12 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,26 @@ public function exists($key)

public function del($key)
{
if (func_num_args() > 1) {
throw new UnsupportedException('In RedisMock, `del` command can not remove more than one key at once.');
if ( is_array($key) ) {
$keys = $key;
}

if (!isset($this->data[$key])) {
return $this->returnPipedInfo(0);
else {
$keys = func_get_args();
}

$deletedItems = count($this->data[$key]);

unset($this->data[$key]);
unset($this->dataTypes[$key]);
if (array_key_exists($key, $this->dataTtl)) {
unset($this->dataTtl[$key]);
$deletedKeyCount = 0;
foreach ( $keys as $k ) {
if ( isset($this->data[$k]) ) {
$deletedKeyCount += count($this->data[$k]);
unset($this->data[$k]);
unset($this->dataTypes[$k]);
if (array_key_exists($k, $this->dataTtl)) {
unset($this->dataTtl[$k]);
}
}
}
return $this->returnPipedInfo($deletedKeyCount);

return $this->returnPipedInfo($deletedItems);
}

public function keys($pattern)
Expand Down
16 changes: 8 additions & 8 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function testSetGetDelExists()
->isEqualTo('OK')
->string($redisMock->set('test2', 'something else'))
->isEqualTo('OK')
->exception(function() use ($redisMock) {
$redisMock->del('test1', 'test2');
})
->isInstanceOf('\M6Web\Component\RedisMock\UnsupportedException')
->integer($redisMock->del('test1'))
->isEqualTo(1)
->integer($redisMock->del('test2'))
->isEqualTo(1)
->integer($redisMock->del('test1', 'test2'))
->isEqualTo(2)
->string($redisMock->set('test1', 'something'))
->isEqualTo('OK')
->string($redisMock->set('test2', 'something else'))
->isEqualTo('OK')
->integer($redisMock->del(array('test1', 'test2')))
->isEqualTo(2)
->string($redisMock->set('test', 'something', 1))
->isEqualTo('OK')
->integer($redisMock->ttl('test'))
Expand Down

0 comments on commit 55ea884

Please sign in to comment.