Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Commit

Permalink
Add HSETNX.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Jan 24, 2011
1 parent 04e277d commit 4266095
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
15 changes: 15 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,21 @@ $redis->hSet('h', 'key1', 'plop'); /* 0, value was replaced. */
$redis->hGet('h', 'key1'); /* returns "plop" */
</pre>

## hSetNx
##### *Description*
Adds a value to the hash stored at key only if this field isn't already in the hash.

##### *Return value*
*BOOL* `TRUE` if the field was set, `FALSE` if it was already present.

##### *Example*
<pre>
$redis->delete('h')
$redis->hSetNx('h', 'key1', 'hello'); /* TRUE, 'key1' => 'hello' in the hash at "h" */
$redis->hSetNx('h', 'key1', 'world'); /* FALSE, 'key1' => 'hello' in the hash at "h". No change since the field wasn't replaced. */
</pre>


## hGet
##### *Description*
Gets a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, `FALSE` is returned.
Expand Down
1 change: 1 addition & 0 deletions php_redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ PHP_METHOD(Redis, rpoplpush);

PHP_METHOD(Redis, hGet);
PHP_METHOD(Redis, hSet);
PHP_METHOD(Redis, hSetNx);
PHP_METHOD(Redis, hDel);
PHP_METHOD(Redis, hLen);
PHP_METHOD(Redis, hKeys);
Expand Down
24 changes: 18 additions & 6 deletions redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ static zend_function_entry redis_functions[] = {
/* 1.2 */
PHP_ME(Redis, hGet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, hSet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, hSetNx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, hDel, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, hLen, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, hKeys, NULL, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -3756,9 +3757,8 @@ PHP_METHOD(Redis, zUnion) {

/* hashes */

/* hSet */
PHP_METHOD(Redis, hSet)
{
PHPAPI void
generic_hset(INTERNAL_FUNCTION_PARAMETERS, char *kw, void (*fun)(INTERNAL_FUNCTION_PARAMETERS, RedisSock *, zval *, void *)) {
zval *object;
RedisSock *redis_sock;
char *key = NULL, *cmd, *member, *val;
Expand All @@ -3778,17 +3778,29 @@ PHP_METHOD(Redis, hSet)

val_free = redis_serialize(redis_sock, z_value, &val, &val_len TSRMLS_CC);
key_free = redis_key_prefix(redis_sock, &key, &key_len);
cmd_len = redis_cmd_format_static(&cmd, "HSET", "sss", key, key_len, member, member_len, val, val_len);
cmd_len = redis_cmd_format_static(&cmd, kw, "sss", key, key_len, member, member_len, val, val_len);
if(val_free) efree(val);
if(key_free) efree(key);

REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
IF_ATOMIC() {
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
fun(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
}
REDIS_PROCESS_RESPONSE(redis_long_response);
REDIS_PROCESS_RESPONSE(fun);
}
/* hSet */
PHP_METHOD(Redis, hSet)
{
generic_hset(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HSET", redis_long_response);
}
/* }}} */
/* hSetNx */
PHP_METHOD(Redis, hSetNx)
{
generic_hset(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HSETNX", redis_1_response);
}
/* }}} */


/* hGet */
PHP_METHOD(Redis, hGet)
Expand Down
9 changes: 9 additions & 0 deletions tests/TestRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,15 @@ public function testHashes() {
$this->redis->hSet('h', 'x', 'a');
$this->redis->hSet('h', 'y', 'b');

// hsetnx
$this->redis->delete('h');
$this->assertTrue(TRUE === $this->redis->hSetNx('h', 'x', 'a'));
$this->assertTrue(TRUE === $this->redis->hSetNx('h', 'y', 'b'));
$this->assertTrue(FALSE === $this->redis->hSetNx('h', 'x', '?'));
$this->assertTrue(FALSE === $this->redis->hSetNx('h', 'y', '?'));
$this->assertTrue('a' === $this->redis->hGet('h', 'x'));
$this->assertTrue('b' === $this->redis->hGet('h', 'y'));

// keys
$keys = $this->redis->hKeys('h');
$this->assertTrue($keys === array('x', 'y') || $keys === array('y', 'x'));
Expand Down

0 comments on commit 4266095

Please sign in to comment.