Skip to content

Commit

Permalink
HINCRBY. Waiting for antirez to implement it :)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Mar 17, 2010
1 parent c59c014 commit 49daf37
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
16 changes: 16 additions & 0 deletions README.markdown
Expand Up @@ -1476,3 +1476,19 @@ $this->hSet('h', 'a', 'x');
$this->hExists('h', 'a'); /* TRUE */
$this->hExists('h', 'NonExistingKey'); /* FALSE */
</pre>

## hIncrBy
##### Description
Increments the value of a member from a hash by a given amount.
##### Parameters
*key*
*value*: (double) value that will be added to the member's value
*member*
##### Return value
*DOUBLE* the new value
##### Examples
<pre>
$redis->delete('h');
$redis->hIncrBy('h', 2.5, 'x'); /* returns 2.5: h[x] = 2.5 now. */
$redis->zIncrBy('h', 1, 'x'); /* h[x] ← 2.5 + 1. Returns 3.5 */
</pre>
1 change: 1 addition & 0 deletions php_redis.h
Expand Up @@ -101,6 +101,7 @@ PHP_METHOD(Redis, hKeys);
PHP_METHOD(Redis, hVals);
PHP_METHOD(Redis, hGetAll);
PHP_METHOD(Redis, hExists);
PHP_METHOD(Redis, hIncrBy);

#ifdef PHP_WIN32
#define PHP_REDIS_API __declspec(dllexport)
Expand Down
22 changes: 17 additions & 5 deletions redis.c
Expand Up @@ -119,6 +119,7 @@ static zend_function_entry redis_functions[] = {
PHP_ME(Redis, hVals, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, hGetAll, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, hExists, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, hIncrBy, NULL, ZEND_ACC_PUBLIC)

/* aliases */
PHP_MALIAS(Redis, open, connect, NULL, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -2970,10 +2971,8 @@ PHP_METHOD(Redis, zScore)
efree(cmd);
redis_bulk_double_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock TSRMLS_CC);
}
/* {{{ proto double Redis::zIncrBy(string key, double value, string member)
*/
PHP_METHOD(Redis, zIncrBy)
{

PHPAPI void generic_incrby_method(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len TSRMLS_DC) {
zval *object;
RedisSock *redis_sock;
char *key = NULL, *cmd, *member, *response;
Expand All @@ -2989,7 +2988,8 @@ PHP_METHOD(Redis, zIncrBy)
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
RETURN_FALSE;
}
cmd_len = redis_cmd_format(&cmd, "ZINCRBY %s %f %d\r\n%s\r\n",
cmd_len = redis_cmd_format(&cmd, "%s %s %f %d\r\n%s\r\n",
keyword, keyword_len,
key, key_len, val, member_len, member, member_len);

if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
Expand All @@ -2999,6 +2999,13 @@ PHP_METHOD(Redis, zIncrBy)
efree(cmd);
redis_bulk_double_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock TSRMLS_CC);
}

/* {{{ proto double Redis::zIncrBy(string key, double value, string member)
*/
PHP_METHOD(Redis, zIncrBy)
{
generic_incrby_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZINCRBY", sizeof("ZINCRBY")-1 TSRMLS_CC);
}
/* }}} */

/* hashes */
Expand Down Expand Up @@ -3304,4 +3311,9 @@ PHP_METHOD(Redis, hGetAll) {
efree(z_ret);

}

PHP_METHOD(Redis, hIncrBy)
{
generic_incrby_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HINCRBY", sizeof("HINCRBY")-1 TSRMLS_CC);
}
/* vim: set tabstop=4 expandtab: */
9 changes: 9 additions & 0 deletions tests/TestRedis.php
Expand Up @@ -1397,6 +1397,15 @@ public function testHashes() {
$this->redis->delete('h');
$this->assertTrue(FALSE === $this->redis->hExists('h', 'x'));

// hIncrBy
$this->redis->delete('h');
$this->assertTrue(2.5 === $this->redis->hIncrBy('h', 2.5, 'x'));
$this->assertTrue(3.5 === $this->redis->hIncrBy('h', 1, 'x'));

$this->redis->hSet('h', 'y', 'not-a-number');
$this->assertTrue(FALSE === $this->redis->hIncrBy('h', 1, 'y'));


}
}

Expand Down

0 comments on commit 49daf37

Please sign in to comment.