Skip to content

Commit

Permalink
Merge pull request #44 from M6Web/feature/hsetnx
Browse files Browse the repository at this point in the history
Add hsetnx command
  • Loading branch information
lnahiro committed Dec 7, 2015
2 parents ff280cb + cfa7403 commit 4ce81d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Redis command | Description
**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
**HSETNX** *key* *field* *value* | Sets field in the hash stored at key to value, only if field does not yet exist
**LINDEX** *key* *index* | Returns the element at index *index* in the list stored at *key*
**LLEN** *key* | Returns the length of the list stored at *key*
**LPUSH** *key* *value* *[value ...]* | Pushs values at the head of a list
Expand Down
21 changes: 21 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,27 @@ public function hset($key, $field, $value)
return $this->returnPipedInfo((int) $isNew);
}

public function hsetnx($key, $field, $value)
{
$this->deleteOnTtlExpired($key);

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

$isNew = !isset(self::$data[$key][$field]);

if ($isNew) {
self::$data[$key][$field] = $value;
self::$dataTypes[$key] = 'hash';
if (array_key_exists($key, self::$dataTtl)) {
unset(self::$dataTtl[$key]);
}
}

return $this->returnPipedInfo((int) $isNew);
}

public function hmset($key, $pairs)
{
$this->deleteOnTtlExpired($key);
Expand Down
11 changes: 11 additions & 0 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,17 @@ public function testHSetHMSetHGetHDelHExistsHGetAll()
'raoul' => null,
'test1' => null,
));
sleep(2);
$this->assert
->integer($redisMock->hsetnx('mykey', 'field', 'my value'))
->isEqualTo(1)
->integer($redisMock->hsetnx('mykey', 'field2', 'second value'))
->isEqualTo(1)
->integer($redisMock->hsetnx('mykey', 'field', 'override value'))
->isEqualTo(0)
->integer($redisMock->del('mykey'))
->isEqualTo(2)
;
}

public function testLLen()
Expand Down

0 comments on commit 4ce81d0

Please sign in to comment.