Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit c24d454

Browse files
committed
do not fetch from cache every time
1 parent be1925f commit c24d454

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/Unleash.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Unleash
2323
protected $config;
2424
protected $request;
2525
protected $features;
26+
protected $expires;
2627

2728
public function __construct(ClientInterface $client, Cache $cache, Config $config, Request $request)
2829
{
@@ -34,13 +35,17 @@ public function __construct(ClientInterface $client, Cache $cache, Config $confi
3435

3536
public function getFeatures(): array
3637
{
38+
if ($this->isFresh()) {
39+
return $this->features;
40+
}
41+
3742
try {
38-
$features = $this->getCachedFeatures();
43+
$this->features = $this->getCachedFeatures();
3944

4045
// Always store the failover cache, in case it is turned on during failure scenarios.
41-
$this->cache->forever('unleash.features.failover', $features);
46+
$this->cache->forever('unleash.features.failover', $this->features);
4247

43-
return $features;
48+
return $this->features;
4449
} catch (TransferException | JsonException $e) {
4550
if ($this->config->get('unleash.cache.failover') === true) {
4651
return $this->cache->get('unleash.features.failover', []);
@@ -110,16 +115,23 @@ public function isFeatureDisabled(string $name, ...$args): bool
110115
return !$this->isFeatureEnabled($name, ...$args);
111116
}
112117

118+
protected function isFresh(): bool
119+
{
120+
return $this->expires > time();
121+
}
122+
113123
protected function getCachedFeatures(): array
114124
{
115125
if (!$this->config->get('unleash.isEnabled')) {
116126
return [];
117127
}
118128

119129
if ($this->config->get('unleash.cache.isEnabled')) {
130+
$this->setExpires();
131+
120132
return $this->cache->remember(
121133
'unleash',
122-
$this->config->get('unleash.cache.ttl', self::DEFAULT_CACHE_TTL),
134+
$this->getCacheTTL(),
123135
function () {
124136
return $this->fetchFeatures();
125137
}
@@ -129,6 +141,16 @@ function () {
129141
return $this->features ?? $this->features = $this->fetchFeatures();
130142
}
131143

144+
protected function getCacheTTL(): int
145+
{
146+
return $this->config->get('unleash.cache.ttl', self::DEFAULT_CACHE_TTL);
147+
}
148+
149+
protected function setExpires(): void
150+
{
151+
$this->expires = $this->getCacheTTL() + time();
152+
}
153+
132154
protected function fetchFeatures(): array
133155
{
134156
$response = $this->client->get($this->config->get('unleash.featuresEndpoint'));
@@ -139,6 +161,8 @@ protected function fetchFeatures(): array
139161
throw new JsonException('Could not decode unleash response body.', $e->getCode(), $e);
140162
}
141163

164+
$this->setExpires();
165+
142166
return $this->formatResponse($data);
143167
}
144168

0 commit comments

Comments
 (0)