Update: 2026-07-02 15:35:12

This commit is contained in:
Hamza-Ayed
2026-07-02 15:35:12 +03:00
parent 2adf195b36
commit 75e6fc42d7
17 changed files with 389 additions and 19 deletions
+34 -2
View File
@@ -4,6 +4,26 @@ require_once __DIR__ . '/../../connect.php';
$type = isset($_POST['type']) ? filterRequest("type") : 'trips';
try {
global $redis;
$redisKey = "gamification_leaderboard:{$type}";
// 1. Try to read from Redis first
if (isset($redis)) {
try {
$cached = $redis->get($redisKey);
if ($cached) {
$data = json_decode($cached, true);
if ($data !== null) {
jsonSuccess($data);
exit;
}
}
} catch (Exception $e) {
error_log("Redis read error in getLeaderboard: " . $e->getMessage());
}
}
// 2. Fallback to Database
if ($type === 'earnings') {
$sql = "
SELECT
@@ -53,8 +73,20 @@ if ($stmt->rowCount() > 0) {
$row['rank'] = $rank++;
}
jsonSuccess($rows);
$responseData = $rows;
} else {
jsonSuccess([]);
$responseData = [];
}
// 3. Save the DB result into Redis for future reads
if (isset($redis)) {
try {
// Cache leaderboard for 1 hour (3600 seconds)
$redis->setex($redisKey, 3600, json_encode($responseData));
} catch (Exception $e) {
error_log("Redis write error in getLeaderboard: " . $e->getMessage());
}
}
jsonSuccess($responseData);
?>