步骤说明

public function addMemberWinNum($memberId, $num = 1)
{
$expirationTime = $this->getExpirationTime();
$existsStatus = $this->redis->exists($this->rankKey);

$res = $this->redis->zIncrBy($this->rankKey, $num, $memberId);
!$existsStatus && $this->redis->expire($this->key, $expirationTime - time());
return $res;
}

/**
* Redis 有序列表排序按字母排序,现分数修改如下
* 分数:整数为分数,小数:最大时间-当前时间
* @param $score
* @return string
*/
public function encodeScore($score)
{
$currentTime = time();
$diffTime = Constants::MAX_TIME - $currentTime;
return (double)"{$score}.{$diffTime}";
}
/**
* 增加用户获胜场次 用于排名
* @param int $memberId 用户
* @param int $winNum 胜利次数
* @return float|string
*/
public function addMemberWinNum(int $memberId, int $winNum = 1)
{
$expirationTime = $this->getExpirationTime();
$existsStatus = $this->redis->exists($this->rankKey);

// 按游戏分值达到时间排序
$oldScore = $this->redis->zScore($this->rankKey, $memberId);
if ($oldScore) {
$this->redis->zAdd($this->rankKey, bcadd($winNum, $oldScore, 10), $memberId);
} else {
$this->redis->zAdd($this->rankKey, $this->redis->encodeScore($winNum), $memberId);
}

// 按游戏参加时间排序
// if ($existsStatus && $this->redis->zExists($this->rankKey, $memberId)) {
// $this->redis->zIncrBy($this->rankKey, $winNum, $memberId);
// } else {
// $this->redis->zAdd($this->rankKey, $this->redis->encodeScore($winNum), $memberId);
// }

!$existsStatus && $this->redis->expire($this->rankKey, $expirationTime - time());
return true;
}

参考文献

Redis Zrevrange 命令