Add trip caching
This commit is contained in:
parent
b56c82fd82
commit
baadb8f982
1 changed files with 36 additions and 13 deletions
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -15,22 +16,44 @@ use GuzzleHttp\Client;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::get('/', function () {
|
Route::get('/', function () {
|
||||||
if (config('app.current_trip_id')) {
|
if (!config('app.current_trip_id')) return view('no-trip');
|
||||||
$client = new Client([
|
|
||||||
'base_uri' => 'https://app.wayward.travel/',
|
|
||||||
'timeout' => 3.0
|
|
||||||
]);
|
|
||||||
|
|
||||||
$response = $client->get('trip/'.config('app.current_trip_id').'/user/zmld8ko6qy7d9j3xvq10/json');
|
$tripFile = config('app.current_trip_id') . '.json';
|
||||||
|
|
||||||
if ($response->getStatusCode() == 200) {
|
if (Storage::disk('local')->exists($tripFile)) {
|
||||||
$data = json_decode($response->getBody());
|
$cachedData = json_decode(Storage::disk('local')->get($tripFile));
|
||||||
return view('tracker', ['trip' => $data->trip]);
|
$cachedDataUpdatedAt = new DateTime($cachedData->trip->updated_at);
|
||||||
} else {
|
$now = new DateTime();
|
||||||
return 'Something went wrong';
|
if (intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600) <= 5) {
|
||||||
|
return view('tracker', ['trip' => $cachedData->trip]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$client = new Client([
|
||||||
|
'base_uri' => 'https://app.wayward.travel/',
|
||||||
|
'timeout' => 3.0
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $client->get('trip/'.config('app.current_trip_id').'/user/zmld8ko6qy7d9j3xvq10/json');
|
||||||
|
|
||||||
|
if ($response->getStatusCode() == 200) {
|
||||||
|
$data = json_decode($response->getBody());
|
||||||
|
|
||||||
|
if (Storage::disk('local')->missing($tripFile)) {
|
||||||
|
Storage::disk('local')->put($tripFile, json_encode($data));
|
||||||
|
} else {
|
||||||
|
$cachedData = json_decode(Storage::disk('local')->get($tripFile));
|
||||||
|
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
|
||||||
|
Storage::disk('local')->put($tripFile, json_encode($data));
|
||||||
|
// TODO: Cache photos locally
|
||||||
|
} else {
|
||||||
|
$data = $cachedData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return view('tracker', ['trip' => $data->trip]);
|
||||||
} else {
|
} else {
|
||||||
return view('no-trip');
|
return 'Something went wrong';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue