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

Commit 93cfafd

Browse files
committed
Handle invalid (legacy) cache data
1 parent 60fd86d commit 93cfafd

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
@@ -241,6 +241,34 @@ protected function findVariant(int $stickiness)
241241

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

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)