Update: 2026-07-02 16:52:25

This commit is contained in:
Hamza-Ayed
2026-07-02 16:52:25 +03:00
parent 75e6fc42d7
commit 5d14493ff4
9 changed files with 213 additions and 20 deletions
@@ -52,15 +52,31 @@ function notifySocketServerDestination($userId, $hasDestination, $destLat = '',
try {
if ($action === 'get') {
$stmtGet = $con->prepare("
SELECT target_latitude, target_longitude, destination_name, created_at
FROM driver_destinations
WHERE driver_id = :did
AND is_active = 1
LIMIT 1
");
$stmtGet->execute([':did' => $user_id]);
$activeDest = $stmtGet->fetch(PDO::FETCH_ASSOC);
// 🟥 Read from Redis FIRST
$activeDest = null;
if (isset($redis)) {
try {
$cached = $redis->get("driver:destination:{$user_id}");
if ($cached) {
$activeDest = json_decode($cached, true);
}
} catch (Exception $e) {
error_log("[save_driver_destination] Redis GET error: " . $e->getMessage());
}
}
// 🟦 Fallback to SQL if Redis miss
if (!$activeDest) {
$stmtGet = $con->prepare("
SELECT target_latitude, target_longitude, destination_name, created_at
FROM driver_destinations
WHERE driver_id = :did
AND is_active = 1
LIMIT 1
");
$stmtGet->execute([':did' => $user_id]);
$activeDest = $stmtGet->fetch(PDO::FETCH_ASSOC);
}
if ($activeDest) {
jsonSuccess($activeDest, "Active destination retrieved.");
@@ -71,6 +87,18 @@ try {
}
if ($action === 'clear') {
// 🟥 Clear from Redis FIRST
if (isset($redis)) {
try {
$redis->del("driver:destination:{$user_id}");
// Remove from the Geo Index (used by GEORADIUS search in pricing engine)
$redis->zRem("geo:driver:destinations", (string)$user_id);
} catch (Exception $e) {
error_log("[save_driver_destination] Redis DEL error: " . $e->getMessage());
}
}
// 🟦 Then clear from SQL
$stmtDeactivate = $con->prepare("
UPDATE driver_destinations
SET is_active = 0
@@ -79,7 +107,7 @@ try {
");
$stmtDeactivate->execute([':did' => $user_id]);
// Sync with Redis on Socket Server
// Sync with Socket Server
notifySocketServerDestination($user_id, 0);
jsonSuccess(null, "تم إلغاء تفعيل الوجهة الشخصية بنجاح.");
@@ -130,7 +158,36 @@ try {
");
$stmtDeactivate->execute([':did' => $user_id]);
// 5. Insert new destination
// 🟥 5a. Write to Redis FIRST (primary read path for pricing engine)
if (isset($redis)) {
try {
$destData = [
'driver_id' => $user_id,
'target_latitude' => (float)$destLat,
'target_longitude' => (float)$destLng,
'destination_name' => $destName,
'is_active' => 1,
'usage_date' => date('Y-m-d')
];
// TTL: end of day (midnight)
$secondsUntilMidnight = strtotime('tomorrow') - time();
// A) Store details as JSON string (for reading driver info)
$redis->setex("driver:destination:{$user_id}", $secondsUntilMidnight, json_encode($destData));
// B) Add to Geo Index for ultra-fast GEORADIUS proximity search
// GEOADD key longitude latitude member
$redis->geoAdd("geo:driver:destinations", (float)$destLng, (float)$destLat, (string)$user_id);
// Geo index has no native TTL per-member, so we set TTL on the whole key if not already set
if ($redis->ttl("geo:driver:destinations") < 0) {
$redis->expire("geo:driver:destinations", $secondsUntilMidnight);
}
} catch (Exception $e) {
error_log("[save_driver_destination] Redis SET error: " . $e->getMessage());
}
}
// 🟦 5b. Insert into SQL as secondary (persistent store)
$stmtInsert = $con->prepare("
INSERT INTO driver_destinations
(driver_id, target_latitude, target_longitude, destination_name, is_active, usage_date)
@@ -143,12 +200,12 @@ try {
':name' => $destName
]);
// Sync with Redis on Socket Server
// Sync with Socket Server
notifySocketServerDestination($user_id, 1, $destLat, $destLng, $destName);
// Increment local Redis counter
if (isset($redis)) {
$redisKey = "driver:dest_count:" . $user_id;
$redisKey = "driver:dest_count:{$user_id}";
$redis->incr($redisKey);
$redis->expire($redisKey, 86400); // 24 hours TTL
}