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

Skip to content

Commit c4fb26e

Browse files
Merge remote-tracking branch 'upstream/master'
2 parents 8283007 + 4169dc2 commit c4fb26e

File tree

4 files changed

+90
-31
lines changed

4 files changed

+90
-31
lines changed

common.h

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ typedef struct {
163163
char *persistent_id;
164164

165165
int serializer;
166+
long dbNumber;
166167

167168
char *prefix;
168169
int prefix_len;

library.c

+29-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
3838
return -1;
3939

4040
eof = php_stream_eof(redis_sock->stream);
41-
while(eof) {
42-
if((MULTI == redis_sock->mode) || redis_sock->watching || count++ == 10) { /* too many failures */
41+
for (; eof; count++) {
42+
if((MULTI == redis_sock->mode) || redis_sock->watching || count == 10) { /* too many failures */
4343
if(redis_sock->stream) { /* close stream if still here */
4444
php_stream_close(redis_sock->stream);
4545
redis_sock->stream = NULL;
@@ -61,6 +61,31 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
6161
eof = php_stream_eof(redis_sock->stream);
6262
}
6363
}
64+
65+
// Reselect the DB.
66+
if (count && redis_sock->dbNumber) {
67+
char *cmd, *response;
68+
int cmd_len, response_len;
69+
70+
cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", redis_sock->dbNumber);
71+
72+
if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
73+
efree(cmd);
74+
return -1;
75+
}
76+
efree(cmd);
77+
78+
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
79+
return -1;
80+
}
81+
82+
if (strncmp(response, "+OK", 3)) {
83+
efree(response);
84+
return -1;
85+
}
86+
efree(response);
87+
}
88+
6489
return 0;
6590
}
6691

@@ -794,6 +819,7 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
794819
redis_sock->stream = NULL;
795820
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
796821
redis_sock->watching = 0;
822+
redis_sock->dbNumber = 0;
797823

798824
redis_sock->persistent = persistent;
799825

@@ -933,6 +959,7 @@ PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC)
933959
return 1;
934960
}
935961

962+
redis_sock->dbNumber = 0;
936963
if (redis_sock->stream != NULL) {
937964
if (!redis_sock->persistent) {
938965
redis_sock_write(redis_sock, "QUIT", sizeof("QUIT") - 1 TSRMLS_CC);

redis.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -3379,6 +3379,8 @@ PHP_METHOD(Redis, select) {
33793379
RETURN_FALSE;
33803380
}
33813381

3382+
redis_sock->dbNumber = dbNumber;
3383+
33823384
cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", dbNumber);
33833385

33843386
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
@@ -5733,7 +5735,7 @@ PHP_METHOD(Redis, config)
57335735

57345736
// Construct an EVAL or EVALSHA command, with option argument array and number of arguments that are keys parameter
57355737
PHPAPI int
5736-
redis_build_eval_cmd(RedisSock *redis_sock, char **ret, char *keyword, char *value, int val_len, zval *args, int keys_count) {
5738+
redis_build_eval_cmd(RedisSock *redis_sock, char **ret, char *keyword, char *value, int val_len, zval *args, int keys_count TSRMLS_DC) {
57375739
zval **elem;
57385740
HashTable *args_hash;
57395741
HashPosition hash_pos;
@@ -5829,7 +5831,7 @@ PHP_METHOD(Redis, evalsha)
58295831
}
58305832

58315833
// Construct our EVALSHA command
5832-
cmd_len = redis_build_eval_cmd(redis_sock, &cmd, "EVALSHA", sha, sha_len, args, keys_count);
5834+
cmd_len = redis_build_eval_cmd(redis_sock, &cmd, "EVALSHA", sha, sha_len, args, keys_count TSRMLS_CC);
58335835

58345836
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
58355837
IF_ATOMIC() {
@@ -5861,7 +5863,7 @@ PHP_METHOD(Redis, eval)
58615863
}
58625864

58635865
// Construct our EVAL command
5864-
cmd_len = redis_build_eval_cmd(redis_sock, &cmd, "EVAL", script, script_len, args, keys_count);
5866+
cmd_len = redis_build_eval_cmd(redis_sock, &cmd, "EVAL", script, script_len, args, keys_count TSRMLS_CC);
58655867

58665868
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
58675869
IF_ATOMIC() {

tests/TestRedis.php

+55-26
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ public function testZX() {
18601860
$this->redis->delete('z');
18611861
$this->redis->zadd('z', 1, 'one');
18621862
$this->redis->zadd('z', 2, 'two');
1863-
$this->redis->zadd('z', 5, 'five');
1863+
$this->redis->zadd('z', 5, 'five');
18641864

18651865
$this->assertTrue(0 === $this->redis->zRank('z', 'one'));
18661866
$this->assertTrue(1 === $this->redis->zRank('z', 'two'));
@@ -2921,29 +2921,29 @@ public function testGetLastError() {
29212921
$this->assertTrue(strlen($this->redis->getLastError()) > 0);
29222922
}
29232923

2924-
// Helper function to compare nested results -- from the php.net array_diff page, I believe
2925-
private function array_diff_recursive($aArray1, $aArray2) {
2926-
$aReturn = array();
2927-
2928-
foreach ($aArray1 as $mKey => $mValue) {
2929-
if (array_key_exists($mKey, $aArray2)) {
2930-
if (is_array($mValue)) {
2931-
$aRecursiveDiff = $this->array_diff_recursive($mValue, $aArray2[$mKey]);
2932-
if (count($aRecursiveDiff)) {
2933-
$aReturn[$mKey] = $aRecursiveDiff;
2934-
}
2935-
} else {
2936-
if ($mValue != $aArray2[$mKey]) {
2937-
$aReturn[$mKey] = $mValue;
2938-
}
2939-
}
2940-
} else {
2941-
$aReturn[$mKey] = $mValue;
2942-
}
2943-
}
2944-
2945-
return $aReturn;
2946-
}
2924+
// Helper function to compare nested results -- from the php.net array_diff page, I believe
2925+
private function array_diff_recursive($aArray1, $aArray2) {
2926+
$aReturn = array();
2927+
2928+
foreach ($aArray1 as $mKey => $mValue) {
2929+
if (array_key_exists($mKey, $aArray2)) {
2930+
if (is_array($mValue)) {
2931+
$aRecursiveDiff = $this->array_diff_recursive($mValue, $aArray2[$mKey]);
2932+
if (count($aRecursiveDiff)) {
2933+
$aReturn[$mKey] = $aRecursiveDiff;
2934+
}
2935+
} else {
2936+
if ($mValue != $aArray2[$mKey]) {
2937+
$aReturn[$mKey] = $mValue;
2938+
}
2939+
}
2940+
} else {
2941+
$aReturn[$mKey] = $mValue;
2942+
}
2943+
}
2944+
2945+
return $aReturn;
2946+
}
29472947

29482948
public function testScript() {
29492949
// Flush any scripts we have
@@ -3114,9 +3114,9 @@ public function testUnserialize() {
31143114

31153115
// Pass them through redis so they're serialized
31163116
foreach($vals as $key => $val) {
3117-
$this->redis->setOption(Redis::OPT_SERIALIZER, $mode);
3117+
$this->redis->setOption(Redis::OPT_SERIALIZER, $mode);
31183118

3119-
$key = "key" . ++$key;
3119+
$key = "key" . ++$key;
31203120
$this->redis->del($key);
31213121
$this->redis->set($key, $val);
31223122

@@ -3148,6 +3148,35 @@ public function testPrefix() {
31483148
$this->redis->setOption(Redis::OPT_PREFIX, '');
31493149

31503150
}
3151+
3152+
public function testReconnectSelect() {
3153+
$key = 'reconnect-select';
3154+
$value = 'Has been set!';
3155+
3156+
$original_cfg = $this->redis->config('GET', 'timeout');
3157+
3158+
// Make sure the default DB doesn't have the key.
3159+
$this->redis->select(0);
3160+
$this->redis->delete($key);
3161+
3162+
// Set the key on a different DB.
3163+
$this->redis->select(5);
3164+
$this->redis->set($key, $value);
3165+
3166+
// Time out after 1 second.
3167+
$this->redis->config('SET', 'timeout', '1');
3168+
3169+
// Wait for the timeout. With Redis 2.4, we have to wait up to 10 s
3170+
// for the server to close the connection, regardless of the timeout
3171+
// setting.
3172+
sleep(11);
3173+
3174+
// Make sure we're still using the same DB.
3175+
$this->assertEquals($value, $this->redis->get($key));
3176+
3177+
// Revert the setting.
3178+
$this->redis->config('SET', 'timeout', $original_cfg['timeout']);
3179+
}
31513180
}
31523181

31533182
TestSuite::run("Redis_Test");

0 commit comments

Comments
 (0)