Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad7de59

Browse files
committed
Merge pull request phpredis#175 from michael-grunder/master
INCRBYFLOAT and HINCRBYFLOAT
2 parents 3160a64 + d66c893 commit ad7de59

File tree

4 files changed

+122
-12
lines changed

4 files changed

+122
-12
lines changed

library.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,16 @@ redis_cmd_format_static(char **ret, char *keyword, char *format, ...) {
278278
smart_str_appendl(&buf, tmp, tmp_len);
279279
}
280280
break;
281+
case 'l':
282+
case 'L': {
283+
long l = va_arg(ap, long);
284+
char tmp[32];
285+
int tmp_len = snprintf(tmp, sizeof(tmp), "%ld", l);
286+
smart_str_append_long(&buf, tmp_len);
287+
smart_str_appendl(&buf, _NL, sizeof(_NL) -1);
288+
smart_str_appendl(&buf, tmp, tmp_len);
289+
}
290+
break;
281291
}
282292
p++;
283293
smart_str_appendl(&buf, _NL, sizeof(_NL) - 1);
@@ -566,6 +576,8 @@ PHPAPI void redis_long_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_s
566576
}
567577
}
568578

579+
580+
569581
PHPAPI int redis_sock_read_multibulk_reply_zipped_with_flag(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, int flag) {
570582

571583
/*

php_redis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ PHP_METHOD(Redis, exists);
4444
PHP_METHOD(Redis, delete);
4545
PHP_METHOD(Redis, incr);
4646
PHP_METHOD(Redis, incrBy);
47+
PHP_METHOD(Redis, incrByFloat);
4748
PHP_METHOD(Redis, decr);
4849
PHP_METHOD(Redis, decrBy);
4950
PHP_METHOD(Redis, type);
@@ -141,6 +142,7 @@ PHP_METHOD(Redis, hVals);
141142
PHP_METHOD(Redis, hGetAll);
142143
PHP_METHOD(Redis, hExists);
143144
PHP_METHOD(Redis, hIncrBy);
145+
PHP_METHOD(Redis, hIncrByFloat);
144146
PHP_METHOD(Redis, hMset);
145147
PHP_METHOD(Redis, hMget);
146148

redis.c

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static zend_function_entry redis_functions[] = {
9191
PHP_ME(Redis, delete, NULL, ZEND_ACC_PUBLIC)
9292
PHP_ME(Redis, incr, NULL, ZEND_ACC_PUBLIC)
9393
PHP_ME(Redis, incrBy, NULL, ZEND_ACC_PUBLIC)
94+
PHP_ME(Redis, incrByFloat, NULL, ZEND_ACC_PUBLIC)
9495
PHP_ME(Redis, decr, NULL, ZEND_ACC_PUBLIC)
9596
PHP_ME(Redis, decrBy, NULL, ZEND_ACC_PUBLIC)
9697
PHP_ME(Redis, type, NULL, ZEND_ACC_PUBLIC)
@@ -176,6 +177,7 @@ static zend_function_entry redis_functions[] = {
176177
PHP_ME(Redis, zUnion, NULL, ZEND_ACC_PUBLIC)
177178
PHP_ME(Redis, zIncrBy, NULL, ZEND_ACC_PUBLIC)
178179
PHP_ME(Redis, expireAt, NULL, ZEND_ACC_PUBLIC)
180+
PHP_ME(Redis, pexpire, NULL, ZEND_ACC_PUBLIC)
179181
PHP_ME(Redis, pexpireAt, NULL, ZEND_ACC_PUBLIC)
180182

181183
/* 1.2 */
@@ -189,6 +191,7 @@ static zend_function_entry redis_functions[] = {
189191
PHP_ME(Redis, hGetAll, NULL, ZEND_ACC_PUBLIC)
190192
PHP_ME(Redis, hExists, NULL, ZEND_ACC_PUBLIC)
191193
PHP_ME(Redis, hIncrBy, NULL, ZEND_ACC_PUBLIC)
194+
PHP_ME(Redis, hIncrByFloat, NULL, ZEND_ACC_PUBLIC)
192195
PHP_ME(Redis, hMset, NULL, ZEND_ACC_PUBLIC)
193196
PHP_ME(Redis, hMget, NULL, ZEND_ACC_PUBLIC)
194197

@@ -1048,6 +1051,38 @@ PHP_METHOD(Redis, incrBy){
10481051
}
10491052
/* }}} */
10501053

1054+
/* {{{ proto float Redis::incrByFloat(string key, float value)
1055+
*/
1056+
PHP_METHOD(Redis, incrByFloat) {
1057+
zval *object;
1058+
RedisSock *redis_sock;
1059+
char *key = NULL, *cmd;
1060+
int key_len, cmd_len, key_free;
1061+
double val;
1062+
1063+
if(zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osd",
1064+
&object, redis_ce, &key, &key_len, &val) == FAILURE) {
1065+
RETURN_FALSE;
1066+
}
1067+
1068+
if(redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0) {
1069+
RETURN_FALSE;
1070+
}
1071+
1072+
// Prefix our key, free it if we have
1073+
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
1074+
if(key_free) efree(key);
1075+
1076+
// Format our INCRBYFLOAT command
1077+
cmd_len = redis_cmd_format_static(&cmd, "INCRBYFLOAT", "sf", key, key_len, val);
1078+
1079+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
1080+
IF_ATOMIC() {
1081+
redis_bulk_double_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
1082+
}
1083+
REDIS_PROCESS_RESPONSE(redis_bulk_double_response);
1084+
}
1085+
10511086
/* {{{ proto boolean Redis::decr(string key [,int value])
10521087
*/
10531088
PHP_METHOD(Redis, decr)
@@ -2858,7 +2893,7 @@ PHPAPI void generic_expire_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int
28582893
}
28592894

28602895
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
2861-
cmd_len = redis_cmd_format_static(&cmd, keyword, "sd", key, key_len, t);
2896+
cmd_len = redis_cmd_format_static(&cmd, keyword, "sl", key, key_len, t);
28622897
if(key_free) efree(key);
28632898

28642899
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
@@ -4506,6 +4541,37 @@ PHPAPI void array_zip_values_and_scores(RedisSock *redis_sock, zval *z_tab, int
45064541
efree(z_ret);
45074542
}
45084543

4544+
PHP_METHOD(Redis, hIncrByFloat)
4545+
{
4546+
zval *object;
4547+
RedisSock *redis_sock;
4548+
char *key = NULL, *cmd, *member;
4549+
int key_len, member_len, cmd_len, key_free;
4550+
double val;
4551+
4552+
// Validate we have the right number of arguments
4553+
if(zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ossd",
4554+
&object, redis_ce,
4555+
&key, &key_len, &member, &member_len, &val) == FAILURE) {
4556+
RETURN_FALSE;
4557+
}
4558+
4559+
// Grab our socket
4560+
if(redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0) {
4561+
RETURN_FALSE;
4562+
}
4563+
4564+
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
4565+
cmd_len = redis_cmd_format_static(&cmd, "HINCRBYFLOAT", "ssf", key, key_len, member, member_len, val);
4566+
if(key_free) efree(key);
4567+
4568+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
4569+
IF_ATOMIC() {
4570+
redis_bulk_double_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
4571+
}
4572+
REDIS_PROCESS_RESPONSE(redis_bulk_double_response);
4573+
}
4574+
45094575
PHP_METHOD(Redis, hIncrBy)
45104576
{
45114577
zval *object;
@@ -4535,8 +4601,6 @@ PHP_METHOD(Redis, hIncrBy)
45354601
}
45364602
}
45374603

4538-
4539-
45404604
/* HINCRBY key member amount */
45414605
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
45424606
cmd_len = redis_cmd_format_static(&cmd, "HINCRBY", "sss", key, key_len, member, member_len, val, val_len);

tests/TestRedis.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function testIncr()
354354
$this->redis->incr('key');
355355
$this->assertEquals(2, (int)$this->redis->get('key'));
356356

357-
$this->redis->incr('key', 3);
357+
$this->redis->incr('key', 3);
358358
$this->assertEquals(5, (int)$this->redis->get('key'));
359359

360360
$this->redis->incrBy('key', 3);
@@ -376,6 +376,28 @@ public function testIncr()
376376
$this->redis->incr('key');
377377
$this->assertTrue("abc" === $this->redis->get('key'));
378378

379+
// incrbyfloat
380+
381+
$this->redis->delete('key');
382+
383+
$this->redis->set('key', 0);
384+
385+
$this->redis->incrbyfloat('key', 1.5);
386+
$this->assertEquals('1.5', $this->redis->get('key'));
387+
388+
$this->redis->incrbyfloat('key', 2.25);
389+
$this->assertEquals('3.75', $this->redis->get('key'));
390+
391+
$this->redis->incrbyfloat('key', -2.25);
392+
$this->assertEquals('1.5', $this->redis->get('key'));
393+
394+
$this->redis->set('key', 'abc');
395+
396+
$this->redis->incrbyfloat('key', 1.5);
397+
$this->assertTrue("abc" === $this->redis->get('key'));
398+
399+
$this->redis->incrbyfloat('key', -1.5);
400+
$this->assertTrue("abc" === $this->redis->get('key'));
379401
}
380402

381403
public function testDecr()
@@ -627,16 +649,16 @@ public function testblockingPop() {
627649
// TODO: fix this broken test.
628650
// $this->redis->delete('list');
629651
// $params = array(
630-
// 0 => array("pipe", "r"),
652+
// 0 => array("pipe", "r"),
631653
// 1 => array("pipe", "w"),
632654
// 2 => array("file", "/dev/null", "w")
633655
// );
634656
// if(function_exists('proc_open')) {
635657
// $env = array('PHPREDIS_key' =>'list', 'PHPREDIS_value' => 'value');
636658
// $process = proc_open('php', $params, $pipes, '/tmp', $env);
637-
//
659+
//
638660
// if (is_resource($process)) {
639-
// fwrite($pipes[0], '<?php
661+
// fwrite($pipes[0], '<?php
640662
// sleep(2);
641663
// $r = new Redis;
642664
// $r->connect("'.self::HOST.'", '.self::PORT.');
@@ -645,11 +667,11 @@ public function testblockingPop() {
645667
// }
646668
// $r->lPush($_ENV["PHPREDIS_key"], $_ENV["PHPREDIS_value"]);
647669
// ?' . '>');
648-
//
670+
//
649671
// fclose($pipes[0]);
650672
// fclose($pipes[1]);
651673
// $re = proc_close($process);
652-
//
674+
//
653675
// $this->assertTrue($this->redis->blPop(array('list'), 5) === array("list", "value"));
654676
// }
655677
// }
@@ -684,8 +706,8 @@ public function testlSize()
684706
}
685707

686708
//lInsert, lPopx, rPopx
687-
public function testlPopx() {
688-
//test lPushx/rPushx
709+
public function testlPopx() {
710+
//test lPushx/rPushx
689711
$this->redis->delete('keyNotExists');
690712
$this->assertTrue($this->redis->lPushx('keyNotExists', 'value') === 0);
691713
$this->assertTrue($this->redis->rPushx('keyNotExists', 'value') === 0);
@@ -1831,7 +1853,7 @@ public function testZX() {
18311853
$this->redis->delete('z');
18321854
$this->redis->zadd('z', 1, 'one');
18331855
$this->redis->zadd('z', 2, 'two');
1834-
$this->redis->zadd('z', 5, 'five');
1856+
$this->redis->zadd('z', 5, 'five');
18351857

18361858
$this->assertTrue(0 === $this->redis->zRank('z', 'one'));
18371859
$this->assertTrue(1 === $this->redis->zRank('z', 'two'));
@@ -1840,6 +1862,7 @@ public function testZX() {
18401862
$this->assertTrue(2 === $this->redis->zRevRank('z', 'one'));
18411863
$this->assertTrue(1 === $this->redis->zRevRank('z', 'two'));
18421864
$this->assertTrue(0 === $this->redis->zRevRank('z', 'five'));
1865+
18431866
}
18441867

18451868
public function testHashes() {
@@ -1909,6 +1932,15 @@ public function testHashes() {
19091932
$this->redis->hSet('h', 'y', 'not-a-number');
19101933
$this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'y', 1));
19111934

1935+
// hIncrByFloat
1936+
$this->redis->delete('h');
1937+
$this->assertTrue(1.5 === $this->redis->hIncrByFloat('h','x', 1.5));
1938+
$this->assertTrue(3.0 === $this->redis->hincrByFloat('h','x', 1.5));
1939+
$this->assertTrue(1.5 === $this->redis->hincrByFloat('h','x', -1.5));
1940+
1941+
$this->redis->hset('h','y','not-a-number');
1942+
$this->assertTrue(FALSE === $this->redis->hIncrByFloat('h', 'y', 1.5));
1943+
19121944
// hmset
19131945
$this->redis->delete('h');
19141946
$this->assertTrue(TRUE === $this->redis->hMset('h', array('x' => 123, 'y' => 456, 'z' => 'abc')));

0 commit comments

Comments
 (0)