Skip to content

Commit b08911c

Browse files
author
Stephen
committed
Added wasCached flag to show it a result was fetched from cache. This can be usefull the rate limits (max 60 per minute)
1 parent a722730 commit b08911c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Cmfcmf/OpenWeatherMap.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,15 @@ class OpenWeatherMap
7070
*/
7171
private $seconds;
7272

73+
/**
74+
* @var Bool True if the last call was fetched from cache, usefull for rate limits
75+
*/
76+
private $wasCached=false;
77+
7378
/**
7479
* @var FetcherInterface The url fetcher.
7580
*/
81+
7682
private $fetcher;
7783

7884
/**
@@ -534,7 +540,9 @@ private function cacheOrFetchResult($url)
534540
/** @var \Cmfcmf\OpenWeatherMap\AbstractCache $cache */
535541
$cache = $this->cacheClass;
536542
$cache->setSeconds($this->seconds);
543+
$this->wasCached=false;
537544
if ($cache->isCached($url)) {
545+
$this->wasCached=true;
538546
return $cache->getCached($url);
539547
}
540548
$result = $this->fetcher->fetch($url);
@@ -584,6 +592,7 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
584592
*/
585593
private function buildQueryUrlParameter($query)
586594
{
595+
587596
switch ($query) {
588597
case (is_array($query) && isset($query['lat']) && isset($query['lon']) && is_numeric($query['lat']) && is_numeric($query['lon'])):
589598
return "lat={$query['lat']}&lon={$query['lon']}";
@@ -595,4 +604,18 @@ private function buildQueryUrlParameter($query)
595604
throw new \InvalidArgumentException('Error: $query has the wrong format. See the documentation of OpenWeatherMap::getRawData() to read about valid formats.');
596605
}
597606
}
607+
608+
/**
609+
* Shows it the last result was fetched from cache
610+
*
611+
*
612+
* @return bool true if last result was fetched from cache, otherwise false.
613+
*
614+
*/
615+
public function wasCached()
616+
{
617+
618+
return $this->wasCached;
619+
}
620+
598621
}

0 commit comments

Comments
 (0)