Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed REDIS_DB is not effected when redis reconnected. #242

Merged
merged 3 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/redis/src/AbstractRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ protected function initRedis()
$error = sprintf('Redis connection authentication failed host=%s port=%d auth=%s', $host, (int)$port, (string)$config['auth']);
throw new RedisException($error);
}
if (isset($config['database']) && $config['database'] < 16 && false === $redis->select($config['database'])) {
$error = sprintf('Redis selection database failure host=%s port=%d database=%d', $host, (int)$port, (int)$config['database']);
throw new RedisException($error);
}

return $redis;
}
Expand Down
3 changes: 0 additions & 3 deletions src/redis/src/Pool/RedisPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public function createConnection(): ConnectionInterface
$redis = new SyncRedisConnection($this);
}

$dbIndex = $this->poolConfig->getDb();
$redis->select($dbIndex);

return $redis;
}
}
4 changes: 4 additions & 0 deletions src/redis/src/RedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function createConnection()
{
$redis = $this->initRedis();
$this->connection = $redis;

/** @var RedisPoolConfig $config */
$config = $this->getPool()->getPoolConfig();
$redis->select($config->getDb());
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/redis/src/SyncRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function createConnection()
$redis->setOption(\Redis::OPT_PREFIX, $prefix);
}
$this->connection = $redis;

/** @var RedisPoolConfig $config */
$config = $this->getPool()->getPoolConfig();
$redis->select($config->getDb());
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/redis/test/Cases/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Swoft\App;
use Swoft\Redis\Exception\RedisException;
use Swoft\Redis\Redis;
use SwoftTest\Redis\Pool\RedisEnvPoolConfig;
use SwoftTest\Redis\Pool\RedisPptPoolConfig;
use SwoftTest\Redis\Testing\Clients\TimeoutRedis;
Expand Down Expand Up @@ -73,4 +74,16 @@ public function testRedisTimeout()
});

}

public function testRedisReconnectSelectDb()
{
$redis = bean(Redis::class);
$redis->set('test_select_db', 1);

$redis->reconnect();

$res = $redis->get('test_select_db');

$this->assertEquals(1, $res);
}
}