Skip to content

Commit

Permalink
Merge pull request BedrockStreaming#19 from M6Web/add-commands
Browse files Browse the repository at this point in the history
add some commands
  • Loading branch information
KuiKui committed Jan 17, 2014
2 parents 40d1dd6 + 97761a2 commit 9d39047
Show file tree
Hide file tree
Showing 3 changed files with 298 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ Redis command | Description
**HEXISTS** *key* *field* | Determines if a hash field exists
**HGET** *key* *field* | Gets the value of a hash field
**HGETALL** *key* | Gets all the fields and values in a hash
**HMSET** *key* *array<field, value>* | Sets each value in the corresponding field
**HSET** *key* *field* *value* | Sets the string value of a hash field
**LPUSH** *key* *value* | Pushs values at the head of a list
**LREM** *key* *count* *value* | Removes `count` instances of `value` from the head of a list
**LTRIM** *key* *start* *stop* | Removes the values of the `key` list which are outside the range `start`...`stop`
**RPUSH** *key* *value* | Pushs values at the tail of a list
**ZADD** *key* *score* *member* | Adds one member to a sorted set, or update its score if it already exists
**ZRANGE** *key* *start* *stop* | Returns the specified range of members in a sorted set
**ZRANGEBYSCORE** *key* *min* *max* *options* | Returns a range of members in a sorted set, by score
Expand Down
126 changes: 126 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,114 @@ public function sismember($key, $member)
return $this->returnPipedInfo(1);
}

// Lists

public function lrem($key, $count, $value)
{
if (!isset($this->data[$key]) || !in_array($value, $this->data[$key]) || $this->deleteOnTtlExpired($key)) {
return $this->returnPipedInfo(0);
}

$arr = $this->data[$key];
$reversed = false;

if ($count < 0) {
$arr = array_reverse($arr);
$count = abs($count);
$reversed = true;
} else if ($count == 0) {
$count = count($arr);
}

$arr = array_filter($arr, function ($curValue) use (&$count, $value) {
if ($count && ($curValue == $value)) {
$count--;
return false;
}

return true;
});

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

if ($reversed) {
$arr = array_reverse($arr);
}

$this->data[$key] = array_values($arr);

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

public function lpush($key, $value)
{
if ($this->deleteOnTtlExpired($key) || !isset($this->data[$key])) {
$this->data[$key] = array();
}

if (isset($this->data[$key]) && !is_array($this->data[$key])) {
return $this->returnPipedInfo(null);
}

array_unshift($this->data[$key], $value);

return $this->returnPipedInfo(count($this->data[$key]));
}

public function rpush($key, $value)
{
if ($this->deleteOnTtlExpired($key) || !isset($this->data[$key])) {
$this->data[$key] = array();
}

if (isset($this->data[$key]) && !is_array($this->data[$key])) {
return $this->returnPipedInfo(null);
}

array_push($this->data[$key], $value);

return $this->returnPipedInfo(count($this->data[$key]));
}

public function ltrim($key, $start, $stop)
{
$this->deleteOnTtlExpired($key);

if (isset($this->data[$key]) && !is_array($this->data[$key])) {
return $this->returnPipedInfo(null);
} elseif (!isset($this->data[$key])) {
return $this->returnPipedInfo('OK');
}

if ($start < 0) {
if (abs($start) > count($this->data[$key])) {
$start = 0;
} else {
$start = count($this->data[$key]) + $start;
}
}

if ($stop >= 0) {
$length = $stop - $start + 1;
} else {
if ($stop == -1) {
$length = NULL;
} else {
$length = $stop + 1;
}
}

$this->data[$key] = array_slice($this->data[$key], $start, $length);

if (!count($this->data[$key])) {
$this->stopPipeline();
$this->del($key);
$this->restorePipeline();
}

return $this->returnPipedInfo('OK');
}

// Hashes

public function hset($key, $field, $value)
Expand All @@ -249,6 +357,24 @@ public function hset($key, $field, $value)
return $this->returnPipedInfo((int) $isNew);
}

public function hmset($key, $pairs)
{
$this->deleteOnTtlExpired($key);

if (isset($this->data[$key]) && !is_array($this->data[$key])) {
return $this->returnPipedInfo(null);
}

$this->stopPipeline();
foreach ($pairs as $field => $value) {
$this->hset($key, $field, $value);
}
$this->restorePipeline();

return $this->returnPipedInfo('OK');
}


public function hget($key, $field)
{
if (!isset($this->data[$key][$field]) || $this->deleteOnTtlExpired($key)) {
Expand Down
168 changes: 167 additions & 1 deletion tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ public function testZRevRangeByScore()
->isEmpty();;
}

public function testHSetHGetHDelHExistsHGetAll()
public function testHSetHMSetHGetHDelHExistsHGetAll()
{
$redisMock = new Redis();

Expand Down Expand Up @@ -775,6 +775,20 @@ public function testHSetHGetHDelHExistsHGetAll()
->isEqualTo(1)
->string($redisMock->type('test'))
->isEqualTo('none')
->string($redisMock->hmset('test', array(
'test1' => 'somthing',
'blabla' => 'anything',
'raoul' => 'nothing',
)))
->isEqualTo('OK')
->array($redisMock->hgetall('test'))
->isEqualTo(array(
'test1' => 'somthing',
'blabla' => 'anything',
'raoul' => 'nothing',
))
->integer($redisMock->del('test'))
->isEqualTo(3)
->exception(function () use ($redisMock) {
$redisMock->hdel('test', 'test1', 'test2');
})
Expand Down Expand Up @@ -822,6 +836,153 @@ public function testHSetHGetHDelHExistsHGetAll()
sleep(2);
$this->assert
->integer($redisMock->hdel('test', 'test1'))
->isEqualTo(0)
->string($redisMock->hmset('test', array(
'test1' => 'somthing',
'blabla' => 'anything',
'raoul' => 'nothing',
)))
->isEqualTo('OK')
->array($redisMock->hgetall('test'))
->isEqualTo(array(
'test1' => 'somthing',
'blabla' => 'anything',
'raoul' => 'nothing',
))
->integer($redisMock->expire('test', 1))
->isEqualTo(1);
sleep(2);
$this->assert
->array($redisMock->hgetall('test'))
->isEmpty();
}

public function testLPushRPushLRemLTrim()
{
$redisMock = new Redis();

$this->assert
->array($redisMock->getData())
->isEmpty()
->integer($redisMock->rpush('test', 'blabla'))
->isIdenticalTo(1)
->integer($redisMock->rpush('test', 'something'))
->isIdenticalTo(2)
->integer($redisMock->rpush('test', 'raoul'))
->isIdenticalTo(3)
->array($redisMock->getData())
->isEqualTo(array('test' => array('blabla', 'something', 'raoul')))
->integer($redisMock->lpush('test', 'raoul'))
->isIdenticalTo(4)
->array($redisMock->getData())
->isEqualTo(array('test' => array('raoul', 'blabla', 'something', 'raoul')))
->integer($redisMock->lrem('test', 2, 'blabla'))
->isIdenticalTo(1)
->array($redisMock->getData())
->isEqualTo(array('test' => array('raoul', 'something', 'raoul')))
->integer($redisMock->lrem('test', 1, 'raoul'))
->isIdenticalTo(1)
->array($redisMock->getData())
->isEqualTo(array('test' => array('something', 'raoul')))
->integer($redisMock->rpush('test', 'raoul'))
->isIdenticalTo(3)
->integer($redisMock->rpush('test', 'raoul'))
->isIdenticalTo(4)
->integer($redisMock->lpush('test', 'raoul'))
->isIdenticalTo(5)
->array($redisMock->getData())
->isEqualTo(array('test' => array('raoul', 'something', 'raoul', 'raoul', 'raoul')))
->integer($redisMock->lrem('test', -2, 'raoul'))
->isIdenticalTo(2)
->array($redisMock->getData())
->isEqualTo(array('test' => array('raoul', 'something', 'raoul')))
->integer($redisMock->rpush('test', 'raoul'))
->isIdenticalTo(4)
->integer($redisMock->rpush('test', 'raoul'))
->isIdenticalTo(5)
->array($redisMock->getData())
->isEqualTo(array('test' => array('raoul', 'something', 'raoul', 'raoul', 'raoul')))
->integer($redisMock->lrem('test', 0, 'raoul'))
->isIdenticalTo(4)
->array($redisMock->getData())
->isEqualTo(array('test' => array('something')))
->integer($redisMock->rpush('test', 'blabla'))
->isIdenticalTo(2)
->integer($redisMock->rpush('test', 'something'))
->isIdenticalTo(3)
->integer($redisMock->rpush('test', 'raoul'))
->isIdenticalTo(4)
->array($redisMock->getData())
->isEqualTo(array('test' => array('something', 'blabla', 'something', 'raoul')))
->string($redisMock->ltrim('test', 0, -1))
->isIdenticalTo('OK')
->array($redisMock->getData())
->isEqualTo(array('test' => array('something', 'blabla', 'something', 'raoul')))
->string($redisMock->ltrim('test', 1, -1))
->isIdenticalTo('OK')
->array($redisMock->getData())
->isEqualTo(array('test' => array('blabla', 'something', 'raoul')))
->string($redisMock->ltrim('test', -2, 2))
->isIdenticalTo('OK')
->array($redisMock->getData())
->isEqualTo(array('test' => array('something', 'raoul')))
->string($redisMock->ltrim('test', 0, 2))
->isIdenticalTo('OK')
->integer($redisMock->lpush('test', 'raoul'))
->isIdenticalTo(3)
->array($redisMock->getData())
->isEqualTo(array('test' => array('raoul', 'something', 'raoul')))
->string($redisMock->ltrim('test', -3, -2))
->isIdenticalTo('OK')
->array($redisMock->getData())
->isEqualTo(array('test' => array('raoul', 'something')))
->string($redisMock->ltrim('test', -1, 0))
->isIdenticalTo('OK')
->boolean($redisMock->exists('test'))
->isIdenticalTo(false)
->integer($redisMock->lpush('test', 'raoul'))
->isIdenticalTo(1)
->array($redisMock->getData())
->isEqualTo(array('test' => array('raoul')))
->integer($redisMock->del('test'))
->isEqualTo(1)
->integer($redisMock->rpush('test', 'test1'))
->isEqualTo(1)
->integer($redisMock->expire('test', 1))
->isEqualTo(1);
sleep(2);
$this->assert
->integer($redisMock->rpush('test', 'test1'))
->isEqualTo(1)
->integer($redisMock->lpush('test', 'test1'))
->isEqualTo(2)
->integer($redisMock->expire('test', 1))
->isEqualTo(1);
sleep(2);
$this->assert
->integer($redisMock->lpush('test', 'test1'))
->isEqualTo(1)
->string($redisMock->ltrim('test', 0, -1))
->isEqualTo('OK')
->integer($redisMock->expire('test', 1))
->isEqualTo(1);
sleep(2);
$this->assert
->string($redisMock->ltrim('test', 0, -1))
->isEqualTo('OK')
->array($redisMock->getData())
->isEmpty()
->integer($redisMock->rpush('test', 'test1'))
->isEqualTo(1)
->integer($redisMock->rpush('test', 'test1'))
->isEqualTo(2)
->integer($redisMock->lrem('test', 1 , 'test1'))
->isEqualTo(1)
->integer($redisMock->expire('test', 1))
->isEqualTo(1);
sleep(2);
$this->assert
->integer($redisMock->lrem('test', 1 , 'test1'))
->isEqualTo(0);
}

Expand Down Expand Up @@ -867,9 +1028,14 @@ public function testPipeline()
->del('test')
->hset('test', 'test1', 'something')
->hget('test', 'test1')
->hmset('test', array('test1' => 'something'))
->hexists('test', 'test1')
->hgetall('test')
->del('test')
->lpush('test', 'test1')
->ltrim('test', 0, -1)
->lrem('test', 1, 'test1')
->rpush('test', 'test1')
->type('test')
->ttl('test')
->expire('test', 1)
Expand Down

0 comments on commit 9d39047

Please sign in to comment.