feat: implement Live Truck Radar caching

This commit is contained in:
ramram1515
2026-07-02 13:02:29 +08:00
parent a1470eb85e
commit ec6b9e61a3
2 changed files with 14 additions and 9 deletions

View File

@@ -20,9 +20,12 @@ class MyLiveTrucksController extends ApiController
*/
public function trucks(Request $request): JsonResponse
{
$household = Household::with('assignedDropOffPoint')
->where('head_user_id', $request->user()->id)
->first();
$userId = $request->user()->id;
$household = \Illuminate\Support\Facades\Cache::remember("user:{$userId}:household", now()->addMinutes(10), function () use ($userId) {
return Household::with('assignedDropOffPoint')
->where('head_user_id', $userId)
->first();
});
if (! $household || ! $household->assigned_drop_off_point_id) {
return $this->ok([

View File

@@ -94,12 +94,14 @@ class TruckTracker
*/
public function activeTruckPositions(): array
{
$trucks = Truck::query()
->where('status', Truck::STATUS_ACTIVE)
->whereNotNull('last_known_coordinates')
->where('last_location_updated_at', '>=', now()->subHour())
->with('assignedTeam')
->get();
$trucks = Cache::remember('active_truck_positions_list', now()->addSeconds(15), function () {
return Truck::query()
->where('status', Truck::STATUS_ACTIVE)
->whereNotNull('last_known_coordinates')
->where('last_location_updated_at', '>=', now()->subHour())
->with('assignedTeam')
->get();
});
return $trucks->map(function (Truck $t) {
$cached = Cache::get($this->cacheKey($t));