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

Commit 98c3006

Browse files
committed
Handle invalid (legacy) cache data
1 parent 059a22f commit 98c3006

File tree

5 files changed

+380
-254
lines changed

5 files changed

+380
-254
lines changed

src/Unleash.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,17 @@ protected function getCachedFeatures(): FeatureFlagCollection
137137
}
138138

139139
if ($this->config->get('unleash.cache.isEnabled')) {
140-
return $this->cache->remember(
140+
$features = $this->cache->remember(
141141
'unleash',
142142
$this->config->get('unleash.cache.ttl', self::DEFAULT_CACHE_TTL),
143143
function () {
144144
return $this->fetchFeatures();
145145
}
146146
);
147+
if ($features instanceof FeatureFlagCollection) {
148+
return $features;
149+
}
150+
$this->cache->forget('unleash');
147151
}
148152

149153
return $this->features ?? $this->features = $this->fetchFeatures();

src/Values/FeatureFlag.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,34 @@ protected function findVariant(int $stickiness)
237237

238238
protected function randomString(): string
239239
{
240-
return bin2hex(random_bytes(random_int(0, 100000)));
240+
return bin2hex(random_bytes(random_int(1, 100000)));
241+
}
242+
243+
public function __serialize(): array
244+
{
245+
return [
246+
'enabled' => $this->enabled,
247+
'name' => $this->name,
248+
'description' => $this->description,
249+
'project' => $this->project,
250+
'stale' => $this->stale,
251+
'type' => $this->type,
252+
'strategies' => $this->strategies->toArray(),
253+
'variants' => $this->variants->toArray(),
254+
];
255+
}
256+
257+
public function __unserialize(array $data): void
258+
{
259+
$this->unleash = resolve(Unleash::class);
260+
$this->enabled = $data['enabled'];
261+
$this->name = $data['name'];
262+
$this->description = $data['description'];
263+
$this->project = $data['project'];
264+
$this->stale = $data['stale'];
265+
$this->type = $data['type'];
266+
267+
$this->strategies = Collection::make($data['strategies']);
268+
$this->variants = Collection::make($data['variants']);
241269
}
242270
}

tests/MockClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88

99
trait MockClient
1010
{
11+
/**
12+
* @var MockHandler
13+
*/
1114
protected $mockHandler;
1215

16+
/**
17+
* @var Client
18+
*/
1319
protected $client;
1420

1521
protected function setUp(): void

0 commit comments

Comments
 (0)