Skip to content

Commit d380358

Browse files
committed
This... this seems to be working?
1 parent 97f0d53 commit d380358

File tree

4 files changed

+127
-8
lines changed

4 files changed

+127
-8
lines changed

app/DataProviders/CachedHafas.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

app/DataProviders/DataProviderBuilder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
class DataProviderBuilder
66
{
7-
public function build(): DataProviderInterface {
7+
public function build(?bool $cache = null): DataProviderInterface {
8+
if ($cache === true || ($cache === null && config('trwl.cache.hafas'))) {
9+
return new CachedHafas();
10+
}
11+
812
return new Hafas();
913
}
1014
}

app/Helpers/CacheKey.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,38 @@ class CacheKey
1919
public const string LEADERBOARD_GLOBAL_DISTANCE = 'LeaderboardGlobalDistance';
2020

2121
// dynamic keys
22-
private const string LEADERBOARD_FRIENDS = 'LeaderboardFriends';
23-
private const string LEADERBOARD_MONTH = 'LeaderboardMonth';
24-
private const string STATISTICS_GLOBAL = 'StatisticsGlobal';
22+
private const string LEADERBOARD_FRIENDS = 'LeaderboardFriends';
23+
private const string LEADERBOARD_MONTH = 'LeaderboardMonth';
24+
private const string STATISTICS_GLOBAL = 'StatisticsGlobal';
25+
private const string HAFAS_TRIP = '_HafasTrip';
26+
private const string HAFAS_STATIONS = '_HafasStations';
27+
private const string HAFAS_DEPARTURES = '_HafasDepartures_%d_%s_%s';
28+
private const string HAFAFS_STATION_RIL = '_HafasStationRil';
29+
private const string HAFAS_STATIONS_FUZZY = '_HafasStationsFuzzy';
2530

2631
// formatting keys
27-
private const string FOR = '%s-for-%s';
28-
private const string FROM_TO = '%s-from-%s-to-%s';
32+
private const string FOR = '%s-for-%s';
33+
private const string FROM_TO = '%s-from-%s-to-%s';
34+
35+
public static function getHafasTripKey(string $tripId): string {
36+
return sprintf(self::FOR, self::HAFAS_TRIP, $tripId);
37+
}
38+
39+
public static function getHafasStationsKey(string $query): string {
40+
return sprintf(self::FOR, self::HAFAS_STATIONS, $query);
41+
}
42+
43+
public static function getHafasDeparturesKey(string $stationId, Carbon $when, bool $localtime): string {
44+
return sprintf(self::HAFAS_DEPARTURES, $stationId, $when->toTimeString(), $localtime ? 'local' : 'utc');
45+
}
46+
47+
public static function getHafasByRilIdentifierKey(string $rilIdentifier): string {
48+
return sprintf(self::FOR, self::HAFAFS_STATION_RIL, $rilIdentifier);
49+
}
50+
51+
public static function getHafasStationsFuzzyKey(string $rilIdentifier): string {
52+
return sprintf(self::FOR, self::HAFAS_STATIONS_FUZZY, $rilIdentifier);
53+
}
2954

3055
public static function getFriendsLeaderboardKey(int $userId): string {
3156
return sprintf(self::FOR, self::LEADERBOARD_FRIENDS, $userId);

config/trwl.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
'mastodon_timeout_seconds' => env("MASTODON_TIMEOUT_SECONDS", 5),
1313

1414
# Brouter
15-
'brouter' => env('BROUTER', true),
15+
'brouter' => env('BROUTER', true),
1616
'brouter_url' => env('BROUTER_URL', 'https://brouter.de/'),
1717
'brouter_timeout' => env('BROUTER_TIMEOUT', 10),
1818

@@ -55,7 +55,8 @@
5555
],
5656
'cache' => [
5757
'global-statistics-retention-seconds' => env('GLOBAL_STATISTICS_CACHE_RETENTION_SECONDS', 60 * 60),
58-
'leaderboard-retention-seconds' => env('LEADERBOARD_CACHE_RETENTION_SECONDS', 5 * 60)
58+
'leaderboard-retention-seconds' => env('LEADERBOARD_CACHE_RETENTION_SECONDS', 5 * 60),
59+
'hafas' => env('HAFAS_CACHE', true),
5960
],
6061
'year_in_review' => [
6162
'alert' => env('YEAR_IN_REVIEW_ALERT', false),

0 commit comments

Comments
 (0)