Skip to content

Commit 691786b

Browse files
Properly apply a prefix on sort(), sortAsc(), and sortDesc() methods with an
included unit test to make sure it's working properly. This fixes issue phpredis#226
1 parent a568029 commit 691786b

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

library.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,8 @@ redis_serialize(RedisSock *redis_sock, zval *z, char **val, int *val_len TSRMLS_
12761276
break;
12771277
}
12781278

1279+
int tvar = EG(precision);
1280+
12791281
/* return string */
12801282
convert_to_string(z_copy);
12811283
*val = Z_STRVAL_P(z_copy);

redis.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,7 +2611,7 @@ PHP_METHOD(Redis, sort) {
26112611

26122612
zval *object = getThis(), *z_array = NULL, **z_cur;
26132613
char *cmd, *old_cmd = NULL, *key;
2614-
int cmd_len, elements = 2, key_len;
2614+
int cmd_len, elements = 2, key_len, key_free;
26152615
int using_store = 0;
26162616
RedisSock *redis_sock;
26172617

@@ -2625,8 +2625,9 @@ PHP_METHOD(Redis, sort) {
26252625
RETURN_FALSE;
26262626
}
26272627

2628-
cmd_len = redis_cmd_format(&cmd, "$4" _NL "SORT" _NL "$%d" _NL "%s" _NL
2629-
, key_len, key, key_len);
2628+
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
2629+
cmd_len = redis_cmd_format(&cmd, "$4" _NL "SORT" _NL "$%d" _NL "%s" _NL, key_len, key, key_len);
2630+
if(key_free) efree(key);
26302631

26312632
if(z_array) {
26322633
if ((zend_hash_find(Z_ARRVAL_P(z_array), "by", sizeof("by"), (void **) &z_cur) == SUCCESS
@@ -2814,7 +2815,7 @@ PHPAPI void generic_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, char *sort, int use_a
28142815
zval *object;
28152816
RedisSock *redis_sock;
28162817
char *key = NULL, *pattern = NULL, *get = NULL, *store = NULL, *cmd;
2817-
int key_len, pattern_len = -1, get_len = -1, store_len = -1, cmd_len;
2818+
int key_len, pattern_len = -1, get_len = -1, store_len = -1, cmd_len, key_free;
28182819
long sort_start = -1, sort_count = -1;
28192820

28202821
int cmd_elements;
@@ -2845,13 +2846,19 @@ PHPAPI void generic_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, char *sort, int use_a
28452846
cmd_lines[2] = estrdup("SORT");
28462847
cmd_sizes[2] = 4;
28472848

2849+
// Prefix our key if we need to
2850+
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
2851+
28482852
/* second line, key */
28492853
cmd_sizes[3] = redis_cmd_format(&cmd_lines[3], "$%d", key_len);
28502854
cmd_lines[4] = emalloc(key_len + 1);
28512855
memcpy(cmd_lines[4], key, key_len);
28522856
cmd_lines[4][key_len] = 0;
28532857
cmd_sizes[4] = key_len;
28542858

2859+
// If we prefixed our key, free it
2860+
if(key_free) efree(key);
2861+
28552862
cmd_elements = 5;
28562863
if(pattern && pattern_len) {
28572864
/* BY */

tests/TestRedis.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,23 @@ public function setupSort() {
782782

783783
}
784784

785+
public function testSortPrefix() {
786+
// Make sure that sorting works with a prefix
787+
$this->redis->setOption(Redis::OPT_PREFIX, 'some-prefix:');
788+
$this->redis->del('some-item');
789+
$this->redis->sadd('some-item', 1);
790+
$this->redis->sadd('some-item', 2);
791+
$this->redis->sadd('some-item', 3);
792+
793+
$this->assertEquals(array('1','2','3'), $this->redis->sortAsc('some-item'));
794+
$this->assertEquals(array('3','2','1'), $this->redis->sortDesc('some-item'));
795+
$this->assertEquals(array('1','2','3'), $this->redis->sort('some-item'));
796+
797+
// Kill our set/prefix
798+
$this->redis->del('some-item');
799+
$this->redis->setOption(Redis::OPT_PREFIX, '');
800+
}
801+
785802
public function testSortAsc() {
786803

787804
$this->setupSort();

0 commit comments

Comments
 (0)