feat : rank
This commit is contained in:
@@ -84,4 +84,13 @@ class ApiRedisKey
|
||||
{
|
||||
return 'chef:leaderboard:city_id:'.$cityId.':type:'.$type.':time_key:'.$timeKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $chefId
|
||||
* @return string
|
||||
*/
|
||||
public static function chefEvaluationKey(int $chefId): string
|
||||
{
|
||||
return 'evaluation:chef:id:'.$chefId;
|
||||
}
|
||||
}
|
||||
57
app/Cache/Redis/Api/EvaluationCache.php
Normal file
57
app/Cache/Redis/Api/EvaluationCache.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Cache\Redis\Api;
|
||||
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class EvaluationCache
|
||||
{
|
||||
/**
|
||||
* @var RedisCache
|
||||
*/
|
||||
#[Inject]
|
||||
protected RedisCache $redis;
|
||||
|
||||
/**
|
||||
* @param int $chefId
|
||||
* @param int $score
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function addChefEvaluation(int $chefId,int $score): void
|
||||
{
|
||||
$key = ApiRedisKey::chefEvaluationKey($chefId);
|
||||
|
||||
$this->redis->hIncrBy($key, 'total_score', $score);
|
||||
$this->redis->hIncrBy($key, 'total_count', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $chefId
|
||||
* @return float|int
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function getChefEvaluation(int $chefId): float|int
|
||||
{
|
||||
$key = ApiRedisKey::chefEvaluationKey($chefId);
|
||||
|
||||
$totalScore = $this->redis->hGet($key, 'total_score');
|
||||
$totalCount = $this->redis->hGet($key, 'total_count');
|
||||
|
||||
if (empty($totalScore) || empty($totalCount)) return 0;
|
||||
|
||||
$avgScore = $totalScore / $totalCount;
|
||||
|
||||
if ($avgScore < 4) {
|
||||
$normalized = $avgScore / 4;
|
||||
$avgScore = 4 + 1 - exp(-2 * $normalized);
|
||||
}
|
||||
|
||||
return round($avgScore,1);
|
||||
}
|
||||
}
|
||||
@@ -409,16 +409,15 @@ class RedisCache
|
||||
|
||||
/**
|
||||
* 单个字段的 hash 值原子增加
|
||||
* @param $key
|
||||
* @param $hashKey
|
||||
* @param $hashValue
|
||||
* @param string $key
|
||||
* @param string $hashKey
|
||||
* @param int $hashValue
|
||||
* @param string $poolName
|
||||
* @return false|int|Redis
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws RedisException
|
||||
*/
|
||||
public function HIncrBy($key, $hashKey, $hashValue, string $poolName = RedisCode::DEFAULT_DB)
|
||||
public function hIncrBy(string $key, string $hashKey, int $hashValue, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
|
||||
{
|
||||
return $this->getRedis($poolName)->hincrby($key, $hashKey, $hashValue);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user