Skip to content

Commit

Permalink
Added cache timeout possibility
Browse files Browse the repository at this point in the history
  • Loading branch information
eldor committed Jun 2, 2021
1 parent 78317fd commit ec87b26
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
9 changes: 8 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ec87b26

Please sign in to comment.