Skip to content

Commit 0c7b5f4

Browse files
committed
Merge branch 'issue.1087' into develop
2 parents d6756ca + c52077b commit 0c7b5f4

File tree

7 files changed

+63
-0
lines changed

7 files changed

+63
-0
lines changed

README.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,7 @@ $redis->migrate('backup', 6379, 'foo', 0, 3600, false, true); /* just REPLACE fl
13551355
* [hSetNx](#hsetnx) - Set the value of a hash field, only if the field does not exist
13561356
* [hVals](#hvals) - Get all the values in a hash
13571357
* [hScan](#hscan) - Scan a hash key for members
1358+
* [hStrLen](#hstrlen) - Get the string length of the value associated with field in the hash
13581359

13591360
### hSet
13601361
-----
@@ -1639,6 +1640,15 @@ while($arr_keys = $redis->hScan('hash', $it)) {
16391640
}
16401641
~~~
16411642

1643+
##### hStrLen
1644+
-----
1645+
**Description**_: Get the string length of the value associated with field in the hash stored at key.
1646+
##### *Parameters*
1647+
*key*: String
1648+
*field*: String
1649+
##### *Return value*
1650+
*LONG* the string length of the value associated with field, or zero when field is not present in the hash or key does not exist at all.
1651+
16421652
## Lists
16431653

16441654
* [blPop, brPop](#blpop-brpop) - Remove and get the first/last element in a list

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ PHP_METHOD(Redis, hIncrBy);
173173
PHP_METHOD(Redis, hIncrByFloat);
174174
PHP_METHOD(Redis, hMset);
175175
PHP_METHOD(Redis, hMget);
176+
PHP_METHOD(Redis, hStrLen);
176177

177178
PHP_METHOD(Redis, multi);
178179
PHP_METHOD(Redis, discard);

redis.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ static zend_function_entry redis_functions[] = {
227227
PHP_ME(Redis, hIncrByFloat, NULL, ZEND_ACC_PUBLIC)
228228
PHP_ME(Redis, hMset, NULL, ZEND_ACC_PUBLIC)
229229
PHP_ME(Redis, hMget, NULL, ZEND_ACC_PUBLIC)
230+
PHP_ME(Redis, hStrLen, NULL, ZEND_ACC_PUBLIC)
230231

231232
PHP_ME(Redis, multi, NULL, ZEND_ACC_PUBLIC)
232233
PHP_ME(Redis, discard, NULL, ZEND_ACC_PUBLIC)
@@ -2237,6 +2238,12 @@ PHP_METHOD(Redis, hMset)
22372238
}
22382239
/* }}} */
22392240

2241+
/* {{{ proto long Redis::hstrlen(string key, string field) */
2242+
PHP_METHOD(Redis, hStrLen) {
2243+
REDIS_PROCESS_CMD(hstrlen, redis_long_response);
2244+
}
2245+
/* }}} */
2246+
22402247
/* flag : get, set {ATOMIC, MULTI, PIPELINE} */
22412248

22422249
PHP_METHOD(Redis, multi)

redis_cluster.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ zend_function_entry redis_cluster_functions[] = {
126126
PHP_ME(RedisCluster, hmset, NULL, ZEND_ACC_PUBLIC)
127127
PHP_ME(RedisCluster, hdel, NULL, ZEND_ACC_PUBLIC)
128128
PHP_ME(RedisCluster, hincrbyfloat, NULL, ZEND_ACC_PUBLIC)
129+
PHP_ME(RedisCluster, hstrlen, NULL, ZEND_ACC_PUBLIC)
129130
PHP_ME(RedisCluster, dump, NULL, ZEND_ACC_PUBLIC)
130131
PHP_ME(RedisCluster, zrank, NULL, ZEND_ACC_PUBLIC)
131132
PHP_ME(RedisCluster, zrevrank, NULL, ZEND_ACC_PUBLIC)
@@ -1468,6 +1469,13 @@ PHP_METHOD(RedisCluster, hmget) {
14681469
}
14691470
/* }}} */
14701471

1472+
/* {{{ proto array RedisCluster::hstrlen(string key, string field) */
1473+
PHP_METHOD(RedisCluster, hstrlen) {
1474+
CLUSTER_PROCESS_CMD(hstrlen, cluster_long_resp, 1);
1475+
}
1476+
/* }}} */
1477+
1478+
14711479
/* {{{ proto string RedisCluster::dump(string key) */
14721480
PHP_METHOD(RedisCluster, dump) {
14731481
CLUSTER_PROCESS_KW_CMD("DUMP", redis_key_cmd, cluster_bulk_raw_resp, 1);

redis_cluster.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ PHP_METHOD(RedisCluster, hincrby);
193193
PHP_METHOD(RedisCluster, hincrbyfloat);
194194
PHP_METHOD(RedisCluster, hset);
195195
PHP_METHOD(RedisCluster, hsetnx);
196+
PHP_METHOD(RedisCluster, hstrlen);
196197
PHP_METHOD(RedisCluster, incr);
197198
PHP_METHOD(RedisCluster, decr);
198199
PHP_METHOD(RedisCluster, incrby);

redis_commands.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,33 @@ int redis_hmset_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
16261626
return SUCCESS;
16271627
}
16281628

1629+
/* HSTRLEN */
1630+
int
1631+
redis_hstrlen_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
1632+
char **cmd, int *cmd_len, short *slot, void **ctx)
1633+
{
1634+
char *key, *field;
1635+
strlen_t key_len, field_len;
1636+
int key_free;
1637+
1638+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key, &key_len,
1639+
&field, &field_len) == FAILURE
1640+
) {
1641+
return FAILURE;
1642+
}
1643+
// Prefix key
1644+
key_free = redis_key_prefix(redis_sock, &key, &key_len);
1645+
1646+
*cmd_len = redis_cmd_format_static(cmd, "HSTRLEN", "ss", key, key_len, field, field_len);
1647+
1648+
// Set our slot
1649+
CMD_SET_SLOT(slot, key, key_len);
1650+
1651+
if (key_free) efree(key);
1652+
1653+
return SUCCESS;
1654+
}
1655+
16291656
/* BITPOS */
16301657
int redis_bitpos_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
16311658
char **cmd, int *cmd_len, short *slot, void **ctx)

tests/RedisTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,15 @@ public function testHashes() {
23572357
$this->assertTrue('Array' === $h1['y']);
23582358
$this->assertTrue('Object' === $h1['z']);
23592359
$this->assertTrue('' === $h1['t']);
2360+
2361+
// hstrlen
2362+
if (version_compare($this->version, '3.2.0', 'ge')) {
2363+
$this->redis->del('h');
2364+
$this->assertTrue(0 === $this->redis->hStrLen('h', 'x')); // key doesn't exist
2365+
$this->redis->hSet('h', 'foo', 'bar');
2366+
$this->assertTrue(0 === $this->redis->hStrLen('h', 'x')); // field is not present in the hash
2367+
$this->assertTrue(3 === $this->redis->hStrLen('h', 'foo'));
2368+
}
23602369
}
23612370

23622371
public function testSetRange() {

0 commit comments

Comments
 (0)