|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\DataProviders; |
| 4 | + |
| 5 | +use App\Enum\TravelType; |
| 6 | +use App\Helpers\CacheKey; |
| 7 | +use App\Models\Station; |
| 8 | +use App\Models\Trip; |
| 9 | +use Carbon\Carbon; |
| 10 | +use Illuminate\Support\Collection; |
| 11 | +use Illuminate\Support\Facades\Cache; |
| 12 | +use Throwable; |
| 13 | + |
| 14 | +class CachedHafas extends Hafas implements DataProviderInterface |
| 15 | +{ |
| 16 | + |
| 17 | + public function fetchHafasTrip(string $tripID, string $lineName): Trip { |
| 18 | + $key = CacheKey::getHafasTripKey($tripID); |
| 19 | + |
| 20 | + return $this->remember($key, now()->addMinutes(15), function() use ($tripID, $lineName) { |
| 21 | + return parent::fetchHafasTrip($tripID, $lineName); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + public function getStations(string $query, int $results = 10): Collection { |
| 26 | + $key = CacheKey::getHafasStationsKey($query); |
| 27 | + |
| 28 | + return $this->remember($key, now()->addMinutes(15), function() use ($query, $results) { |
| 29 | + return parent::getStations($query, $results); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + public function getDepartures(Station $station, Carbon $when, int $duration = 15, TravelType $type = null, bool $localtime = false): Collection { |
| 34 | + $filterWhen = clone $when; |
| 35 | + $when = clone $when; |
| 36 | + $when->subMinutes(2); |
| 37 | + // set cache when minutes to 0, 15, 30 or 45 |
| 38 | + $when->minute = floor($when->minute / 15) * 15; |
| 39 | + $when->second = 0; |
| 40 | + |
| 41 | + // set duration longer than 15 minutes |
| 42 | + $duration = $duration < 15 ? 30 : $duration; |
| 43 | + |
| 44 | + $key = CacheKey::getHafasDeparturesKey($station->id, $when, $localtime); |
| 45 | + |
| 46 | + $departures = $this->remember($key, now()->addMinutes(15), function() use ($station, $when, $duration, $type, $localtime) { |
| 47 | + return parent::getDepartures($station, $when, $duration, $type, $localtime); |
| 48 | + }); |
| 49 | + |
| 50 | + // filter entries by when and duration |
| 51 | + return $departures->filter(function($departure) use ($filterWhen, $duration) { |
| 52 | + $depWhen = Carbon::parse($departure->when); |
| 53 | + return $depWhen->between($filterWhen, $filterWhen->copy()->addMinutes($duration)); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + public function getStationByRilIdentifier(string $rilIdentifier): ?Station { |
| 58 | + $key = CacheKey::getHafasByRilIdentifierKey($rilIdentifier); |
| 59 | + |
| 60 | + return $this->remember($key, now()->addMinutes(15), function() use ($rilIdentifier) { |
| 61 | + return parent::getStationByRilIdentifier($rilIdentifier); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + public function getStationsByFuzzyRilIdentifier(string $rilIdentifier): ?Collection { |
| 66 | + $key = CacheKey::getHafasStationsFuzzyKey($rilIdentifier); |
| 67 | + |
| 68 | + return $this->remember($key, now()->addMinutes(15), function() use ($rilIdentifier) { |
| 69 | + return parent::getStationsByFuzzyRilIdentifier($rilIdentifier); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + private function remember(string $key, Carbon $expires, callable $callback): mixed { |
| 74 | + if (Cache::has($key)) { |
| 75 | + CacheKey::increment('hafas_cache_hit'); |
| 76 | + return Cache::get($key); |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + $result = $callback(); |
| 81 | + Cache::put($key, $result, $expires); |
| 82 | + CacheKey::increment('hafas_cache_set'); |
| 83 | + return $result; |
| 84 | + } catch (Throwable $e) { |
| 85 | + Cache::put($key, null, $expires); |
| 86 | + throw $e; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments