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

Skip to content

Commit c52077b

Browse files
committed
`hStrLen` command
1 parent 12a3335 commit c52077b

7 files changed

+63
-0
lines changed

README.markdown

+10
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

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ PHP_METHOD(Redis, hIncrBy);
174174
PHP_METHOD(Redis, hIncrByFloat);
175175
PHP_METHOD(Redis, hMset);
176176
PHP_METHOD(Redis, hMget);
177+
PHP_METHOD(Redis, hStrLen);
177178

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

redis.c

+7
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ static zend_function_entry redis_functions[] = {
230230
PHP_ME(Redis, hIncrByFloat, NULL, ZEND_ACC_PUBLIC)
231231
PHP_ME(Redis, hMset, NULL, ZEND_ACC_PUBLIC)
232232
PHP_ME(Redis, hMget, NULL, ZEND_ACC_PUBLIC)
233+
PHP_ME(Redis, hStrLen, NULL, ZEND_ACC_PUBLIC)
233234

234235
PHP_ME(Redis, multi, NULL, ZEND_ACC_PUBLIC)
235236
PHP_ME(Redis, discard, NULL, ZEND_ACC_PUBLIC)
@@ -2192,6 +2193,12 @@ PHP_METHOD(Redis, hMset)
21922193
}
21932194
/* }}} */
21942195

2196+
/* {{{ proto long Redis::hstrlen(string key, string field) */
2197+
PHP_METHOD(Redis, hStrLen) {
2198+
REDIS_PROCESS_CMD(hstrlen, redis_long_response);
2199+
}
2200+
/* }}} */
2201+
21952202
/* flag : get, set {ATOMIC, MULTI, PIPELINE} */
21962203

21972204
PHP_METHOD(Redis, multi)

redis_cluster.c

+8
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)
@@ -1472,6 +1473,13 @@ PHP_METHOD(RedisCluster, hmget) {
14721473
}
14731474
/* }}} */
14741475

1476+
/* {{{ proto array RedisCluster::hstrlen(string key, string field) */
1477+
PHP_METHOD(RedisCluster, hstrlen) {
1478+
CLUSTER_PROCESS_CMD(hstrlen, cluster_long_resp, 1);
1479+
}
1480+
/* }}} */
1481+
1482+
14751483
/* {{{ proto string RedisCluster::dump(string key) */
14761484
PHP_METHOD(RedisCluster, dump) {
14771485
CLUSTER_PROCESS_KW_CMD("DUMP", redis_key_cmd, cluster_bulk_raw_resp, 1);

redis_cluster.h

+1
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

+27
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

+9
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,15 @@ public function testHashes() {
23542354
$this->assertTrue('Array' === $h1['y']);
23552355
$this->assertTrue('Object' === $h1['z']);
23562356
$this->assertTrue('' === $h1['t']);
2357+
2358+
// hstrlen
2359+
if (version_compare($this->version, '3.2.0', 'ge')) {
2360+
$this->redis->del('h');
2361+
$this->assertTrue(0 === $this->redis->hStrLen('h', 'x')); // key doesn't exist
2362+
$this->redis->hSet('h', 'foo', 'bar');
2363+
$this->assertTrue(0 === $this->redis->hStrLen('h', 'x')); // field is not present in the hash
2364+
$this->assertTrue(3 === $this->redis->hStrLen('h', 'foo'));
2365+
}
23572366
}
23582367

23592368
public function testSetRange() {

0 commit comments

Comments
 (0)