Skip to content

Commit cccc399

Browse files
Update EXISTS to handle multiple keys
Fixes phpredis#1223
1 parent 6e83c11 commit cccc399

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

redis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ PHP_METHOD(Redis, getMultiple)
15621562
*/
15631563
PHP_METHOD(Redis, exists)
15641564
{
1565-
REDIS_PROCESS_KW_CMD("EXISTS", redis_key_cmd, redis_1_response);
1565+
REDIS_PROCESS_CMD(exists, redis_long_response);
15661566
}
15671567
/* }}} */
15681568

redis_cluster.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ PHP_METHOD(RedisCluster, getset) {
10081008

10091009
/* {{{ proto int RedisCluster::exists(string key) */
10101010
PHP_METHOD(RedisCluster, exists) {
1011-
CLUSTER_PROCESS_KW_CMD("EXISTS", redis_key_cmd, cluster_1_resp, 1);
1011+
CLUSTER_PROCESS_CMD(exists, cluster_long_resp, 1);
10121012
}
10131013
/* }}} */
10141014

redis_commands.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,6 +2790,14 @@ int redis_migrate_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
27902790
return SUCCESS;
27912791
}
27922792

2793+
/* EXISTS */
2794+
int redis_exists_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
2795+
char **cmd, int *cmd_len, short *slot, void **ctx)
2796+
{
2797+
return gen_varkey_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock,
2798+
"EXISTS", sizeof("EXISTS") - 1, 0, 0, cmd, cmd_len, slot);
2799+
}
2800+
27932801
/* DEL */
27942802
int redis_del_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
27952803
char **cmd, int *cmd_len, short *slot, void **ctx)

redis_commands.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ int redis_object_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
201201
REDIS_REPLY_TYPE *rtype, char **cmd, int *cmd_len, short *slot,
202202
void **ctx);
203203

204+
int redis_exists_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
205+
char **cmd, int *cmd_len, short *slot, void **ctx);
206+
204207
int redis_del_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
205208
char **cmd, int *cmd_len, short *slot, void **ctx);
206209

tests/RedisArrayTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ public function testMultiExecDel() {
460460
->exec();
461461

462462
$this->assertTrue($out[0] === 2);
463-
$this->assertTrue($this->ra->exists('1_{employee:joe}_group') === FALSE);
464-
$this->assertTrue($this->ra->exists('1_{employee:joe}_salary') === FALSE);
463+
$this->assertEquals(0, $this->ra->exists('1_{employee:joe}_group'));
464+
$this->assertEquals(0, $this->ra->exists('1_{employee:joe}_salary'));
465465
}
466466

467467
public function testDiscard() {

tests/RedisTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function testGetSet() {
354354
public function testRandomKey() {
355355
for($i = 0; $i < 1000; $i++) {
356356
$k = $this->redis->randomKey();
357-
$this->assertTrue($this->redis->exists($k));
357+
$this->assertEquals($this->redis->exists($k), 1);
358358
}
359359
}
360360

@@ -555,7 +555,7 @@ public function testIncrByFloat()
555555
$this->redis->incrbyfloat('key',1.8);
556556
$this->assertEquals(1.8, floatval($this->redis->get('key'))); // convert to float to avoid rounding issue on arm
557557
$this->redis->setOption(Redis::OPT_PREFIX, '');
558-
$this->assertTrue($this->redis->exists('someprefix:key'));
558+
$this->assertEquals(1, $this->redis->exists('someprefix:key'));
559559
$this->redis->del('someprefix:key');
560560

561561
}
@@ -586,10 +586,25 @@ public function testDecr()
586586

587587
public function testExists()
588588
{
589+
/* Single key */
589590
$this->redis->del('key');
590-
$this->assertFalse($this->redis->exists('key'));
591+
$this->assertEquals(0, $this->redis->exists('key'));
591592
$this->redis->set('key', 'val');
592-
$this->assertEquals(True, $this->redis->exists('key'));
593+
$this->assertEquals(1, $this->redis->exists('key'));
594+
595+
/* Add multiple keys */
596+
$mkeys = [];
597+
for ($i = 0; $i < 10; $i++) {
598+
if (rand(1, 2) == 1) {
599+
$mkey = "{exists}key:$i";
600+
$this->redis->set($mkey, $i);
601+
$mkeys[] = $mkey;
602+
}
603+
}
604+
605+
/* Test passing an array as well as the keys variadic */
606+
$this->assertEquals(count($mkeys), $this->redis->exists($mkeys));
607+
$this->assertEquals(count($mkeys), call_user_func_array([$this->redis, 'exists'], $mkeys));
593608
}
594609

595610
public function testGetKeys()
@@ -4089,7 +4104,7 @@ private function checkSerializer($mode) {
40894104
$this->redis->sAdd('k', 'a', 'b', 'c', 'd');
40904105
$this->assertTrue(2 === $this->redis->sRem('k', 'a', 'd'));
40914106
$this->assertTrue(2 === $this->redis->sRem('k', 'b', 'c', 'e'));
4092-
$this->assertTrue(FALSE === $this->redis->exists('k'));
4107+
$this->assertEquals(0, $this->redis->exists('k'));
40934108

40944109
// sismember
40954110
$this->assertTrue(TRUE === $this->redis->sismember('{set}key', $s[0]));

0 commit comments

Comments
 (0)