Skip to content

Commit d63f07b

Browse files
authored
Merge pull request #58 from VincentLanglet/fix/flag-call
Fix invalid cache value
2 parents f1f7f04 + f0316e1 commit d63f07b

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/Flagsmith.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -491,22 +491,28 @@ private function cachedCall(
491491
return $this->call($method, $uri, $body);
492492
}
493493

494-
//If $skipCache, or skipCache(), or the key does not exist then call the API
495-
if ($skipCache || $this->skipCache() || !$this->cache->has($cacheKey)) {
496-
try {
497-
$response = $this->call($method, $uri, $body);
498-
$this->cache->set($cacheKey, $response, $ttl);
499-
} catch (FlagsmithAPIError $e) {
500-
if (
501-
!$this->useCacheAsFailover ||
502-
!$this->cache->has($cacheKey)
503-
) {
504-
throw $e;
505-
}
494+
if (!$skipCache && !$this->skipCache()) {
495+
$cachedCall = $this->cache->get($cacheKey);
496+
if (\is_array($cachedCall) || \is_object($cachedCall)) {
497+
return $cachedCall;
506498
}
507499
}
508500

509-
return $this->cache->get($cacheKey);
501+
try {
502+
$response = $this->call($method, $uri, $body);
503+
$this->cache->set($cacheKey, $response, $ttl);
504+
505+
return $response;
506+
} catch (FlagsmithAPIError $e) {
507+
if ($this->useCacheAsFailover) {
508+
$cachedCall = $this->cache->get($cacheKey);
509+
if (\is_array($cachedCall) || \is_object($cachedCall)) {
510+
return $cachedCall;
511+
}
512+
}
513+
514+
throw $e;
515+
}
510516
}
511517

512518
/**

0 commit comments

Comments
 (0)