feat: implement Live Truck Radar caching
This commit is contained in:
@@ -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([
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user