Update: 2026-07-04 18:43:56
This commit is contained in:
@@ -61,13 +61,24 @@ try {
|
||||
continue;
|
||||
}
|
||||
|
||||
$validDrivers[] = [
|
||||
'id' => $d_id,
|
||||
'distance' => $d_dist,
|
||||
'heading' => $profile['heading'] ?? 0,
|
||||
'speed' => $profile['speed'] ?? 0,
|
||||
'lat' => $d_coord[1], // Latitude من الريدز (أدق)
|
||||
'lng' => $d_coord[0] // Longitude من الريدز
|
||||
// 🆕 فحص driver:public cache (بيانات السائق الثابتة)
|
||||
$public = $redis->hgetall("driver:public:$d_id");
|
||||
$hasCache = !empty($public) && !empty($public['first_name']);
|
||||
|
||||
$validDrivers[] = $hasCache ? array_merge($public, [
|
||||
'id' => $d_id,
|
||||
'driver_id' => $d_id,
|
||||
'distance' => $d_dist,
|
||||
'cached' => true
|
||||
]) : [
|
||||
'id' => $d_id,
|
||||
'driver_id' => $d_id,
|
||||
'distance' => $d_dist,
|
||||
'heading' => $profile['heading'] ?? 0,
|
||||
'speed' => $profile['speed'] ?? 0,
|
||||
'latitude' => $d_coord[1],
|
||||
'longitude' => $d_coord[0],
|
||||
'cached' => false
|
||||
];
|
||||
|
||||
if (count($validDrivers) >= $limit) break;
|
||||
|
||||
@@ -230,6 +230,10 @@ $io->on('workerStart', function () use ($io, $INTERNAL_KEY) {
|
||||
|
||||
if (isset($ops['hmset'])) {
|
||||
$pipe->hmset($profileKey, $ops['hmset']);
|
||||
// 🆕 Cache public driver data للقراءة السريعة من get.php (24h TTL)
|
||||
$publicKey = "driver:public:$driverId";
|
||||
$pipe->hmset($publicKey, $ops['hmset']);
|
||||
$pipe->expire($publicKey, 86400);
|
||||
}
|
||||
if (isset($ops['expire'])) {
|
||||
$pipe->expire($profileKey, $ops['expire']);
|
||||
@@ -418,6 +422,34 @@ $io->on('workerStart', function () use ($io, $INTERNAL_KEY) {
|
||||
logMsg("✅ Ride #$rideId taken by #$winnerDriverId.");
|
||||
$connection->send('OK');
|
||||
|
||||
// ── 7. Update Ride State (Redis Cache فقط — بدون Forward)
|
||||
} elseif ($action === 'update_ride_state') {
|
||||
$rideId = $post['ride_id'] ?? null;
|
||||
$status = $post['status'] ?? '';
|
||||
$driverId = $post['driver_id'] ?? '';
|
||||
$passengerId = $post['passenger_id'] ?? '';
|
||||
|
||||
if (!$rideId || !$status) {
|
||||
$connection->send('Error: Missing ride_id or status');
|
||||
return;
|
||||
}
|
||||
|
||||
if ($redis) {
|
||||
$stateKey = "ride:$rideId:state";
|
||||
$stateData = [
|
||||
'status' => $status,
|
||||
'driver_id' => $driverId,
|
||||
'passenger_id' => $passengerId,
|
||||
'updated_at' => time(),
|
||||
];
|
||||
$redis->hmset($stateKey, $stateData);
|
||||
$redis->expire($stateKey, 86400);
|
||||
|
||||
logMsg("🚗 Ride #$rideId → status: $status (cached in Redis)");
|
||||
}
|
||||
|
||||
$connection->send('OK');
|
||||
|
||||
// ── 5. Force Disconnect ───────────────────────────────
|
||||
} elseif ($action === 'force_disconnect') {
|
||||
$driverId = $post['driver_id'] ?? null;
|
||||
@@ -467,6 +499,20 @@ $io->on('workerStart', function () use ($io, $INTERNAL_KEY) {
|
||||
}
|
||||
$connection->send('OK');
|
||||
|
||||
// ── 7. Cache Driver Public Data ─────────────────────────
|
||||
} elseif ($action === 'cache_driver_public') {
|
||||
$driverId = $post['driver_id'] ?? null;
|
||||
$data = json_decode($post['data'] ?? '[]', true);
|
||||
|
||||
if ($driverId && $redis && !empty($data)) {
|
||||
$key = "driver:public:$driverId";
|
||||
$redis->hmset($key, $data);
|
||||
$redis->expire($key, 86400);
|
||||
$connection->send('OK');
|
||||
} else {
|
||||
$connection->send('Error');
|
||||
}
|
||||
|
||||
} else {
|
||||
$connection->send('Unknown action');
|
||||
}
|
||||
@@ -631,7 +677,9 @@ $io->on('connection', function ($socket) use ($INTERNAL_KEY) {
|
||||
|
||||
if ($needHmset) {
|
||||
$eventBuffer[$driverId]['hmset'] = [
|
||||
'id' => $driverId, 'heading' => $heading, 'speed' => $speed, 'status' => $status, 'updated_at' => $now
|
||||
'id' => $driverId, 'lat' => $lat, 'lng' => $lng,
|
||||
'heading' => $heading, 'speed' => $speed,
|
||||
'status' => $status, 'updated_at' => $now
|
||||
];
|
||||
$state['speed'] = $speedMs;
|
||||
$state['heading'] = $heading;
|
||||
@@ -667,6 +715,13 @@ $io->on('connection', function ($socket) use ($INTERNAL_KEY) {
|
||||
if ($needGeoadd) {
|
||||
$state['lat'] = $lat;
|
||||
$state['lng'] = $lng;
|
||||
// 🆕 تحديث الموقع في driver:public (حتى لو ما تغير speed/heading)
|
||||
if (!isset($eventBuffer[$driverId]['hmset'])) {
|
||||
$eventBuffer[$driverId]['hmset'] = [
|
||||
'id' => $driverId, 'lat' => $lat, 'lng' => $lng,
|
||||
'updated_at' => $now
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user