Skip to content

Commit eda670a

Browse files
fix(cache): Use serialize/unserialize in FileCache for object persistence
This resolves issues where `ClientStateManager` would fail to hydrate `ClientState` objects from `FileCache`, effectively making the cache non-functional for object storage.
1 parent 7b25d5d commit eda670a

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

src/Defaults/FileCache.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,11 @@ private function readCacheFile(): array
193193

194194
$handle = @fopen($this->cacheFile, 'rb');
195195
if ($handle === false) {
196-
// TODO: Log error
197196
return [];
198197
}
199198

200199
try {
201200
if (! flock($handle, LOCK_SH)) {
202-
// TODO: Log error
203201
return [];
204202
}
205203
$content = stream_get_contents($handle);
@@ -209,9 +207,8 @@ private function readCacheFile(): array
209207
return [];
210208
}
211209

212-
$data = json_decode($content, true);
213-
if (json_last_error() !== JSON_ERROR_NONE || ! is_array($data)) {
214-
// TODO: Log error, potentially unlink corrupt file
210+
$data = unserialize($content);
211+
if ($data === false) {
215212
return [];
216213
}
217214

@@ -225,29 +222,25 @@ private function readCacheFile(): array
225222

226223
private function writeCacheFile(array $data): bool
227224
{
228-
$jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
229-
if (json_last_error() !== JSON_ERROR_NONE) {
230-
// TODO: Log error
225+
$jsonData = serialize($data);
226+
227+
if ($jsonData === false) {
231228
return false;
232229
}
233230

234231
$handle = @fopen($this->cacheFile, 'cb');
235232
if ($handle === false) {
236-
// TODO: Log error
237233
return false;
238234
}
239235

240236
try {
241237
if (! flock($handle, LOCK_EX)) {
242-
// TODO: Log error
243238
return false;
244239
}
245240
if (! ftruncate($handle, 0)) {
246-
// TODO: Log error
247241
return false;
248242
}
249243
if (fwrite($handle, $jsonData) === false) {
250-
// TODO: Log error
251244
return false;
252245
}
253246
fflush($handle);
@@ -256,7 +249,6 @@ private function writeCacheFile(array $data): bool
256249

257250
return true;
258251
} catch (Throwable $e) {
259-
// TODO: Log error
260252
flock($handle, LOCK_UN); // Ensure lock release on error
261253

262254
return false;
@@ -271,7 +263,6 @@ private function ensureDirectoryExists(string $directory): void
271263
{
272264
if (! is_dir($directory)) {
273265
if (! @mkdir($directory, $this->dirPermission, true)) {
274-
// TODO: Log error
275266
throw new InvalidArgumentException("Cache directory does not exist and could not be created: {$directory}");
276267
}
277268
@chmod($directory, $this->dirPermission);
@@ -291,11 +282,9 @@ private function calculateExpiry(DateInterval|int|null $ttl): ?int
291282
try {
292283
return (new DateTimeImmutable())->add($ttl)->getTimestamp();
293284
} catch (Throwable $e) {
294-
// TODO: Log error
295285
return null;
296286
}
297287
}
298-
// TODO: Log warning
299288
throw new InvalidArgumentException('Invalid TTL type provided. Must be null, int, or DateInterval.');
300289
}
301290

@@ -321,9 +310,9 @@ private function validateKeys(array $keys): void
321310
{
322311
foreach ($keys as $key) {
323312
if (! is_string($key)) {
324-
throw new InvalidArgumentException('Cache key must be a string, got '.gettype($key));
313+
throw new InvalidArgumentException('Cache key must be a string, got ' . gettype($key));
325314
}
326-
$this->sanitizeKey($key); // Reuse sanitize validation
315+
$this->sanitizeKey($key);
327316
}
328317
}
329318

0 commit comments

Comments
 (0)