diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2021-02-11 11:48:52 +0100 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2021-02-11 11:48:52 +0100 |
commit | bf4210aa8d43d7d4bc8e4edfd10735c7f5ffe4a5 (patch) | |
tree | dc9566085c88c784880727c6e5f11f3567e682c3 /apps/weather_status/lib | |
parent | d5dea10517bbceaf141f56b6dde0efb55fc6f4b5 (diff) | |
download | nextcloud-server-bf4210aa8d43d7d4bc8e4edfd10735c7f5ffe4a5.tar.gz nextcloud-server-bf4210aa8d43d7d4bc8e4edfd10735c7f5ffe4a5.zip |
Always use the cache
If there is no cache available we will use the null cache. Which (you
guessed it) just doesn't do anything. This makes the code flow a bit
nicer and psalm a bit happier.
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps/weather_status/lib')
-rw-r--r-- | apps/weather_status/lib/Service/WeatherStatusService.php | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/apps/weather_status/lib/Service/WeatherStatusService.php b/apps/weather_status/lib/Service/WeatherStatusService.php index 0d7b31f9042..86b77a4358f 100644 --- a/apps/weather_status/lib/Service/WeatherStatusService.php +++ b/apps/weather_status/lib/Service/WeatherStatusService.php @@ -72,9 +72,6 @@ class WeatherStatusService { /** @var IAppManager */ private $appManager; - /** @var ICacheFactory */ - private $cacheFactory; - /** @var ICache */ private $cache; @@ -116,9 +113,7 @@ class WeatherStatusService { $this->version = $appManager->getAppVersion(Application::APP_ID); $this->clientService = $clientService; $this->client = $clientService->newClient(); - if ($cacheFactory->isAvailable()) { - $this->cache = $cacheFactory->createDistributed(); - } + $this->cache = $cacheFactory->createDistributed('weatherstatus'); } /** @@ -397,12 +392,12 @@ class WeatherStatusService { * @return array which contains the error message or the parsed JSON result */ private function requestJSON(string $url, array $params = []): array { - if (isset($this->cache)) { - $cacheKey = $url . '|' . implode(',', $params) . '|' . implode(',', array_keys($params)); - if ($this->cache->hasKey($cacheKey)) { - return $this->cache->get($cacheKey); - } + $cacheKey = $url . '|' . implode(',', $params) . '|' . implode(',', array_keys($params)); + $cacheValue = $this->cache->get($cacheKey); + if ($cacheValue !== null) { + return $cacheValue; } + try { $options = [ 'headers' => [ @@ -425,20 +420,20 @@ class WeatherStatusService { return ['error' => $this->l10n->t('Error')]; } else { $json = json_decode($body, true); - if (isset($this->cache)) { - // default cache duration is one hour - $cacheDuration = 60 * 60; - if (isset($headers['Expires']) && count($headers['Expires']) > 0) { - // if the Expires response header is set, use it to define cache duration - $expireTs = (new \Datetime($headers['Expires'][0]))->getTimestamp(); - $nowTs = (new \Datetime())->getTimestamp(); - $duration = $expireTs - $nowTs; - if ($duration > $cacheDuration) { - $cacheDuration = $duration; - } + + // default cache duration is one hour + $cacheDuration = 60 * 60; + if (isset($headers['Expires']) && count($headers['Expires']) > 0) { + // if the Expires response header is set, use it to define cache duration + $expireTs = (new \Datetime($headers['Expires'][0]))->getTimestamp(); + $nowTs = (new \Datetime())->getTimestamp(); + $duration = $expireTs - $nowTs; + if ($duration > $cacheDuration) { + $cacheDuration = $duration; } - $this->cache->set($cacheKey, $json, $cacheDuration); } + $this->cache->set($cacheKey, $json, $cacheDuration); + return $json; } } catch (\Exception $e) { |