Skip to content

Commit

Permalink
Change cached filename
Browse files Browse the repository at this point in the history
  • Loading branch information
dfridrich committed Dec 9, 2023
1 parent 7cc7570 commit 8fff989
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/OpenWeather.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct($cacheDir = null, $apiKey = null)
public function getForecast($city = null, $lang = 'cz', $units = self::UNITS_METRIC)
{
$city = null === $city ? self::DEFAULT_CITY : $city;
$cachedFileName = md5(date('YmdH').$city.$lang.$units).'.json';
$cachedFileName = sprintf("%s_%s.json", date('YmdH'), hash('crc32', $city.$lang.$units));

if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir);
Expand All @@ -64,7 +64,9 @@ public function getForecast($city = null, $lang = 'cz', $units = self::UNITS_MET
if (!is_file($cachedFile)) {
$url = sprintf(self::API_URL, urlencode($city), $lang, $units, $this->apiKey);
$json = json_decode(file_get_contents($url));
file_put_contents($cachedFile, json_encode($json, JSON_PRETTY_PRINT));
if (!empty($json)) {
file_put_contents($cachedFile, json_encode($json, JSON_PRETTY_PRINT));
}
} else {
$json = json_decode(file_get_contents($cachedFile));
}
Expand Down

0 comments on commit 8fff989

Please sign in to comment.