diff options
-rw-r--r-- | README.markdown | 10 | ||||
-rw-r--r-- | php_redis.h | 1 | ||||
-rw-r--r-- | redis.c | 7 | ||||
-rw-r--r-- | redis_cluster.c | 8 | ||||
-rw-r--r-- | redis_cluster.h | 1 | ||||
-rw-r--r-- | redis_commands.c | 27 | ||||
-rw-r--r-- | tests/RedisTest.php | 9 |
7 files changed, 63 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown index e66f5899..5431e32c 100644 --- a/README.markdown +++ b/README.markdown @@ -1355,6 +1355,7 @@ $redis->migrate('backup', 6379, 'foo', 0, 3600, false, true); /* just REPLACE fl * [hSetNx](#hsetnx) - Set the value of a hash field, only if the field does not exist * [hVals](#hvals) - Get all the values in a hash * [hScan](#hscan) - Scan a hash key for members +* [hStrLen](#hstrlen) - Get the string length of the value associated with field in the hash ### hSet ----- @@ -1639,6 +1640,15 @@ while($arr_keys = $redis->hScan('hash', $it)) { } ~~~ +##### hStrLen +----- + **Description**_: Get the string length of the value associated with field in the hash stored at key. +##### *Parameters* +*key*: String +*field*: String +##### *Return value* +*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. + ## Lists * [blPop, brPop](#blpop-brpop) - Remove and get the first/last element in a list diff --git a/php_redis.h b/php_redis.h index ef665ea6..635e42e1 100644 --- a/php_redis.h +++ b/php_redis.h @@ -173,6 +173,7 @@ PHP_METHOD(Redis, hIncrBy); PHP_METHOD(Redis, hIncrByFloat); PHP_METHOD(Redis, hMset); PHP_METHOD(Redis, hMget); +PHP_METHOD(Redis, hStrLen); PHP_METHOD(Redis, multi); PHP_METHOD(Redis, discard); @@ -227,6 +227,7 @@ static zend_function_entry redis_functions[] = { PHP_ME(Redis, hIncrByFloat, NULL, ZEND_ACC_PUBLIC) PHP_ME(Redis, hMset, NULL, ZEND_ACC_PUBLIC) PHP_ME(Redis, hMget, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Redis, hStrLen, NULL, ZEND_ACC_PUBLIC) PHP_ME(Redis, multi, NULL, ZEND_ACC_PUBLIC) PHP_ME(Redis, discard, NULL, ZEND_ACC_PUBLIC) @@ -2237,6 +2238,12 @@ PHP_METHOD(Redis, hMset) } /* }}} */ +/* {{{ proto long Redis::hstrlen(string key, string field) */ +PHP_METHOD(Redis, hStrLen) { + REDIS_PROCESS_CMD(hstrlen, redis_long_response); +} +/* }}} */ + /* flag : get, set {ATOMIC, MULTI, PIPELINE} */ PHP_METHOD(Redis, multi) diff --git a/redis_cluster.c b/redis_cluster.c index 44c5e0e2..869434e4 100644 --- a/redis_cluster.c +++ b/redis_cluster.c @@ -126,6 +126,7 @@ zend_function_entry redis_cluster_functions[] = { PHP_ME(RedisCluster, hmset, NULL, ZEND_ACC_PUBLIC) PHP_ME(RedisCluster, hdel, NULL, ZEND_ACC_PUBLIC) PHP_ME(RedisCluster, hincrbyfloat, NULL, ZEND_ACC_PUBLIC) + PHP_ME(RedisCluster, hstrlen, NULL, ZEND_ACC_PUBLIC) PHP_ME(RedisCluster, dump, NULL, ZEND_ACC_PUBLIC) PHP_ME(RedisCluster, zrank, NULL, ZEND_ACC_PUBLIC) PHP_ME(RedisCluster, zrevrank, NULL, ZEND_ACC_PUBLIC) @@ -1468,6 +1469,13 @@ PHP_METHOD(RedisCluster, hmget) { } /* }}} */ +/* {{{ proto array RedisCluster::hstrlen(string key, string field) */ +PHP_METHOD(RedisCluster, hstrlen) { + CLUSTER_PROCESS_CMD(hstrlen, cluster_long_resp, 1); +} +/* }}} */ + + /* {{{ proto string RedisCluster::dump(string key) */ PHP_METHOD(RedisCluster, dump) { CLUSTER_PROCESS_KW_CMD("DUMP", redis_key_cmd, cluster_bulk_raw_resp, 1); diff --git a/redis_cluster.h b/redis_cluster.h index 34689429..6b4bb0d8 100644 --- a/redis_cluster.h +++ b/redis_cluster.h @@ -193,6 +193,7 @@ PHP_METHOD(RedisCluster, hincrby); PHP_METHOD(RedisCluster, hincrbyfloat); PHP_METHOD(RedisCluster, hset); PHP_METHOD(RedisCluster, hsetnx); +PHP_METHOD(RedisCluster, hstrlen); PHP_METHOD(RedisCluster, incr); PHP_METHOD(RedisCluster, decr); PHP_METHOD(RedisCluster, incrby); diff --git a/redis_commands.c b/redis_commands.c index 1566c0e5..6e8a1086 100644 --- a/redis_commands.c +++ b/redis_commands.c @@ -1626,6 +1626,33 @@ int redis_hmset_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, return SUCCESS; } +/* HSTRLEN */ +int +redis_hstrlen_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, + char **cmd, int *cmd_len, short *slot, void **ctx) +{ + char *key, *field; + strlen_t key_len, field_len; + int key_free; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key, &key_len, + &field, &field_len) == FAILURE + ) { + return FAILURE; + } + // Prefix key + key_free = redis_key_prefix(redis_sock, &key, &key_len); + + *cmd_len = redis_cmd_format_static(cmd, "HSTRLEN", "ss", key, key_len, field, field_len); + + // Set our slot + CMD_SET_SLOT(slot, key, key_len); + + if (key_free) efree(key); + + return SUCCESS; +} + /* BITPOS */ int redis_bitpos_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char **cmd, int *cmd_len, short *slot, void **ctx) diff --git a/tests/RedisTest.php b/tests/RedisTest.php index 132a894e..53cf6e9d 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -2357,6 +2357,15 @@ class Redis_Test extends TestSuite $this->assertTrue('Array' === $h1['y']); $this->assertTrue('Object' === $h1['z']); $this->assertTrue('' === $h1['t']); + + // hstrlen + if (version_compare($this->version, '3.2.0', 'ge')) { + $this->redis->del('h'); + $this->assertTrue(0 === $this->redis->hStrLen('h', 'x')); // key doesn't exist + $this->redis->hSet('h', 'foo', 'bar'); + $this->assertTrue(0 === $this->redis->hStrLen('h', 'x')); // field is not present in the hash + $this->assertTrue(3 === $this->redis->hStrLen('h', 'foo')); + } } public function testSetRange() { |