Skip to content

Commit 7f653b8

Browse files
committed
Added hGet/hSet + tests & documentation.
1 parent e5e1e70 commit 7f653b8

4 files changed

Lines changed: 150 additions & 0 deletions

File tree

README.markdown

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,3 +1296,35 @@ $redis->zIncrBy('key', 2.5, 'member1'); /* key or member1 didn't exist, so membe
12961296
/* and now has the value 2.5 */
12971297
$redis->zIncrBy('key', 1, 'member1'); /* 3.5 */
12981298
</pre>
1299+
1300+
1301+
## hSet
1302+
##### *Description*
1303+
Adds a value to the hash stored at key. If this value is already in the hash, `FALSE` is returned.
1304+
##### *Parameters*
1305+
*key*
1306+
*hashKey*
1307+
*value*
1308+
1309+
##### *Return value*
1310+
*LONG* `TRUE` if value didn't exist and was added successfully, `FALSE` if the value was already present and was replaced.
1311+
##### *Example*
1312+
<pre>
1313+
$redis->delete('h')
1314+
$redis->hSet('h', 'key1', 'hello'); /* TRUE, 'key1' => 'hello' in the hash at "h" */
1315+
$redis->hGet('h', 'key1'); /* returns "hello" */
1316+
1317+
$redis->hSet('h', 'key1', 'plop'); /* FALSE, value was replaced. */
1318+
$redis->hGet('h', 'key1'); /* returns "plop" */
1319+
</pre>
1320+
1321+
## hGet
1322+
##### *Description*
1323+
Gets a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, `FALSE` is returned.
1324+
##### *Parameters*
1325+
*key*
1326+
*hashKey*
1327+
1328+
##### *Return value*
1329+
*STRING* The value, if the command executed successfully
1330+
*BOOL* `FALSE` in case of failure (empty list)

php_redis.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ PHP_METHOD(Redis, expireAt);
9393
PHP_METHOD(Redis, mset);
9494
PHP_METHOD(Redis, rpoplpush);
9595

96+
PHP_METHOD(Redis, hGet);
97+
PHP_METHOD(Redis, hSet);
98+
9699
#ifdef PHP_WIN32
97100
#define PHP_REDIS_API __declspec(dllexport)
98101
#else

redis.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ static zend_function_entry redis_functions[] = {
110110
PHP_ME(Redis, zIncrBy, NULL, ZEND_ACC_PUBLIC)
111111
PHP_ME(Redis, expireAt, NULL, ZEND_ACC_PUBLIC)
112112

113+
/* 1.2 */
114+
PHP_ME(Redis, hGet, NULL, ZEND_ACC_PUBLIC)
115+
PHP_ME(Redis, hSet, NULL, ZEND_ACC_PUBLIC)
116+
117+
113118
/* aliases */
114119
PHP_MALIAS(Redis, open, connect, NULL, ZEND_ACC_PUBLIC)
115120
PHP_MALIAS(Redis, lLen, lSize, NULL, ZEND_ACC_PUBLIC)
@@ -2955,4 +2960,96 @@ PHP_METHOD(Redis, zIncrBy)
29552960
}
29562961
/* }}} */
29572962

2963+
/* hashes */
2964+
2965+
/* hGet */
2966+
PHP_METHOD(Redis, hSet)
2967+
{
2968+
zval *object;
2969+
RedisSock *redis_sock;
2970+
char *key = NULL, *cmd, *member, *val;
2971+
int key_len, member_len, cmd_len, val_len;
2972+
2973+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osss",
2974+
&object, redis_ce,
2975+
&key, &key_len, &member, &member_len, &val, &val_len) == FAILURE) {
2976+
RETURN_FALSE;
2977+
}
2978+
2979+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
2980+
RETURN_FALSE;
2981+
}
2982+
cmd_len = redis_cmd_format(&cmd,
2983+
"*4\r\n"
2984+
"$4\r\n"
2985+
"HSET\r\n"
2986+
2987+
"$%d\r\n" /* key */
2988+
"%s\r\n"
2989+
2990+
"$%d\r\n" /* field */
2991+
"%s\r\n"
2992+
2993+
"$%d\r\n" /* value */
2994+
"%s\r\n"
2995+
,
2996+
key_len, key, key_len,
2997+
member_len, member, member_len,
2998+
val_len, val, val_len);
2999+
3000+
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
3001+
efree(cmd);
3002+
RETURN_FALSE;
3003+
}
3004+
efree(cmd);
3005+
3006+
redis_1_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock TSRMLS_CC);
3007+
}
3008+
/* }}} */
3009+
3010+
/* hSet */
3011+
PHP_METHOD(Redis, hGet)
3012+
{
3013+
zval *object;
3014+
RedisSock *redis_sock;
3015+
char *key = NULL, *cmd, *member, *response;
3016+
int key_len, member_len, cmd_len, response_len;
3017+
3018+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
3019+
&object, redis_ce,
3020+
&key, &key_len, &member, &member_len) == FAILURE) {
3021+
RETURN_FALSE;
3022+
}
3023+
3024+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
3025+
RETURN_FALSE;
3026+
}
3027+
cmd_len = redis_cmd_format(&cmd,
3028+
"*3\r\n"
3029+
"$4\r\n"
3030+
"HGET\r\n"
3031+
3032+
"$%d\r\n" /* key */
3033+
"%s\r\n"
3034+
3035+
"$%d\r\n" /* field */
3036+
"%s\r\n"
3037+
,
3038+
key_len, key, key_len,
3039+
member_len, member, member_len);
3040+
3041+
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
3042+
efree(cmd);
3043+
RETURN_FALSE;
3044+
}
3045+
efree(cmd);
3046+
3047+
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
3048+
RETURN_FALSE;
3049+
}
3050+
3051+
RETURN_STRINGL(response, response_len, 0);
3052+
}
3053+
/* }}} */
3054+
29583055
/* vim: set tabstop=4 expandtab: */

tests/TestRedis.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,24 @@ public function testZX() {
13461346
$this->assertTrue(2.5 === $this->redis->zIncrBy('key', 1.5, 'val1'));
13471347
$this->assertTrue(2.5 === $this->redis->zScore('key', 'val1'));
13481348
}
1349+
1350+
public function testHashes() {
1351+
$this->redis->delete('h', 'key');
1352+
1353+
$this->assertTrue(TRUE === $this->redis->hSet('h', 'a', 'a-value'));
1354+
$this->assertTrue(TRUE === $this->redis->hSet('h', 'b', 'b-value'));
1355+
1356+
$this->assertTrue('a-value' === $this->redis->hGet('h', 'a')); // simple get
1357+
$this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get
1358+
1359+
$this->assertTrue(FALSE === $this->redis->hSet('h', 'a', 'another-value')); // replacement
1360+
$this->assertTrue('another-value' === $this->redis->hGet('h', 'a')); // get the new value
1361+
1362+
$this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get
1363+
$this->assertTrue(FALSE === $this->redis->hGet('h', 'c')); // unknown hash member
1364+
$this->assertTrue(FALSE === $this->redis->hGet('key', 'c')); // unknownkey
1365+
1366+
}
13491367
}
13501368

13511369
?>

0 commit comments

Comments
 (0)