我predis
用来与两个 Redis 实例(主服务器和从服务器)交互。Master主要用于只写,slave用于只读。
我不得不使用很好select
的功能来切换连接Redis
,但我担心它的性能。我最好创建一个单例类并将其用于实例化$this->redis
以及select
如何保持持久性?
到目前为止,以下是我的一些代码。我想知道是否可以这样做以提高性能?
$redis = $this->redis;
$tipperKey = str_pad($tipperId, 10, '0', STR_PAD_LEFT);
if ($this->debug) {
$this->logger->addDebug('tipperKey=' . $tipperKey);
$this->logger->addDebug("Adding groupId:$groupId to \"groups:$tipperKey\" in db:0");
}
// add groupId to tipper groups set
try {
$redis->select(0);
$redis->sadd('groups:' . $tipperKey, $groupId);
} catch (Exception $e) {
$this->logger->addError("Error adding groupId:$groupId to \"groups:$tipperKey\" in db:0 : " . $e->getMessage());
return false;
}
$result = $this->getRoundsPlayed($competitionId); // cached (600)
if (!$result) {
return false;
}
$tk = 'tipper:' . $tipperKey;
try {
foreach ($result as $row) {
$i = (int)$row['round_number'];
if ($this->debug) {
$this->logger->addDebug('select db:' . $i);
}
$redis->select($i);
$sportId = (int)$row['sport_id'];
顺便说一句,这是为 Gearman Job 准备的