Skip to content

Commit

Permalink
#517 #518 reduce cache validity for journey references and compositions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertware committed May 2, 2024
1 parent 095ddc2 commit c5b8d41
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
9 changes: 5 additions & 4 deletions app/Http/Controllers/DatedVehicleJourneyV1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@

class DatedVehicleJourneyV1Controller extends BaseIrailController
{
private VehicleJourneyRepository $vehicleJourneyRepository;

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
public function __construct(VehicleJourneyRepository $vehicleJourneyRepository)
{
//
$this->vehicleJourneyRepository = $vehicleJourneyRepository;
}

public function getVehicleById(DatedVehicleJourneyV1Request $request): Response
{
$repo = app(VehicleJourneyRepository::class);
$vehicleJourneySearchResult = $repo->getDatedVehicleJourney($request);
$vehicleJourneySearchResult = $this->vehicleJourneyRepository->getDatedVehicleJourney($request);
$dataRoot = DatedVehicleJourneyV1Converter::convert($request, $vehicleJourneySearchResult);
$this->logRequest($request);
return $this->outputV1($request, $dataRoot);
Expand Down
6 changes: 2 additions & 4 deletions app/Repositories/Nmbs/NmbsRivCompositionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,8 @@ private function fetchCompositionData(Vehicle $vehicle, JourneyWithOriginAndDest
// Planning data often lacks detail. Store it for 5 minutes
$this->setCachedObject($cacheKey, $compositionData, 5 * 60);
} else {
// Confirmed data doesn't change and contains all details. This data dispersal after the train ride,
// so cache it long enough so it doesn't disappear instantly after the ride.
// TODO: data should not be cached for too long into the next day, or a departure date should be added to the query
$this->setCachedObject($cacheKey, $compositionData, 60 * 60 * 6);
// Confirmed data can still change, but less often
$this->setCachedObject($cacheKey, $compositionData, 3600);
}
} catch (CompositionUnavailableException $e) {
// Cache "data unavailable" for 5 minutes to limit outgoing requests. Only do this after a fresh attempts,
Expand Down
2 changes: 1 addition & 1 deletion app/Repositories/Riv/NmbsRivRawDataRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function () use ($request) {
return false;
}
},
6 * 3600
2 * 3600 // Journey detail references seem to change in some cases, so don't cache them for too long
);

if ($cachedJourneyDetailRef->getValue() === false) {
Expand Down

0 comments on commit c5b8d41

Please sign in to comment.