From ec87b267ba8fed9be343847328bd54fc15e9727f Mon Sep 17 00:00:00 2001 From: Henrik B Hansen Date: Wed, 2 Jun 2021 11:32:09 +0200 Subject: [PATCH] Added cache timeout possibility --- README.md | 7 +++++++ src/Providers/Provider.php | 2 +- src/Request.php | 9 ++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index af74f8f..d76cf54 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,13 @@ Start by setting up your request $request = (new Vemcogroup\Weather\Request('1 Infinite Loop, Cupertino, CA 95014, USA')); ``` +By default, it caches the weather response for 24hrs (86.400sec), this can be changed by setting a second parameter to cache timeout (in seconds) + +```php +$request = (new Vemcogroup\Weather\Request('1 Infinite Loop, Cupertino, CA 95014, USA', 600)); +``` + + *Units* There two available unit types, default is Metric: diff --git a/src/Providers/Provider.php b/src/Providers/Provider.php index b4bd6cc..89e6a78 100644 --- a/src/Providers/Provider.php +++ b/src/Providers/Provider.php @@ -70,7 +70,7 @@ protected function processRequests(): void yield $this->client->getAsync($request->getUrl())->then(function (GuzzleResponse $response) use ($request) { $content = json_decode($response->getBody()); - Cache::put(md5('laravel-weather-' . $request->getUrl()), $content, now()->addDay()); + Cache::put(md5('laravel-weather-' . $request->getUrl()), $content, $request->getCacheTimeout()); $request->setResponse($content); }); } diff --git a/src/Request.php b/src/Request.php index cacb2be..af36b21 100644 --- a/src/Request.php +++ b/src/Request.php @@ -20,12 +20,14 @@ class Request private $geocode; private $options; private $response; + private $cacheTimeout; - public function __construct(string $address) + public function __construct(string $address, $cacheTimeout = 86400) { $this->dates = []; $this->options = []; $this->address = $address; + $this->cacheTimeout = $cacheTimeout; $this->units = 's'; $this->locale = 'en'; @@ -69,6 +71,11 @@ public function getCacheResponse(): ?object return Cache::get(md5('laravel-weather-' . $this->url)); } + public function getCacheTimeout() + { + return $this->cacheTimeout; + } + public function getAddress(): string { return $this->address;